Database Exercise: College Management System
Objective:
To create a simple relational database that demonstrates:
One-to-One
One-to-Many
Many-to-Many relationships
Create:
1. One-to-One Relationship: Student and Locker
Entities:
Students(StudentID, FirstName, LastName, Semester)
Lockers(LockerID, LockerNumber, StudentID)
Records:
Students Table:
StudentID FirstName LastName Semester
1 Alice Kimani 1
2 Bob Otieno 2
3 Charlie Mutiso 3
Lockers Table:
LockerID LockerNumber StudentID
1 L101 1
2 L102 2
3 L103 3
Task:
Page 1 of 5
Create both tables.
Ensure that each locker is assigned to only one student.
Create a one-to-one relationship between Students.StudentID and Lockers.StudentID.
2. One-to-Many Relationship: Department and Lecturers
Entities:
Departments(DeptID, DeptName)
Lecturers(LecturerID, LecturerName, DeptID)
Records:
Departments Table:
DeptID DeptName
1 Computer Science
2 Mathematics
Lecturers Table:
LecturerID LecturerName DeptID
1 Dr. Kariuki 1
2 Prof. Wanjiku 1
3 Dr. Ochieng 2
Task:
Create both tables.
Each department has many lecturers, but each lecturer belongs to one department.
Set up a one-to-many relationship from Departments.DeptID to Lecturers.DeptID.
3. Many-to-Many Relationship: Students and Courses
Page 2 of 5
Entities:
Students(StudentID, Name)
Courses(CourseID, CourseName)
Junction Table: Enrollments(EnrollmentID, StudentID, CourseID, Grade)
Records:
Courses Table:
CourseID CourseName
1 Databases
2 Calculus
Enrollments Table:
EnrollmentID StudentID CourseID Grade
1 1 1 85
2 1 2 78
3 2 1 90
4 3 2 88
Task:
Create the three tables.
Use the Enrollments table to handle the many-to-many relationship.
Each student can enroll in many courses, and each course can have many students.
Set foreign key relationships from Enrollments to both Students and Courses.
Queries:
1. List all students and their locker numbers.
2. List all lecturers in the "Computer Science" department.
Page 3 of 5
3. Find all courses taken by a specific student (e.g., StudentID = 101).
4. Show the names of students who scored more than 80 in any course.
5. Display departments and the count of lecturers in each.
Queries Answers:
1. List all students and their locker numbers
SELECT Students.FirstName, Students.LastName, Lockers.LockerNumber
FROM Students
INNER JOIN Lockers ON Students.StudentID = Lockers.StudentID;
This joins the Students and Lockers tables using the StudentID.
2. List all lecturers in the "Computer Science" department
SELECT Lecturers.LecturerName, Departments.DeptName
FROM Lecturers
INNER JOIN Departments ON Lecturers.DeptID = Departments.DeptID
WHERE Departments.DeptName = "Computer Science";
This retrieves all lecturers whose DeptID points to the Computer Science department.
3. Find all courses taken by a specific student (e.g., StudentID = 101)
SELECT Students.FirstName, Students.LastName, Courses.CourseName
FROM Students
INNER JOIN (Enrollments
INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID)
ON Students.StudentID = Enrollments.StudentID
WHERE Students.StudentID = 101;
This gets the course names associated with a specific student's enrollment.
4. Show the names of students who scored more than 80 in any course
SELECT Students.FirstName, Students.LastName, Courses.CourseName, Enrollments.Grade
FROM (Students
Page 4 of 5
INNER JOIN Enrollments ON Students.StudentID = Enrollments.StudentID)
INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID
WHERE Enrollments.Grade > 80;
This pulls all students and courses where the grade is above 80.
5. Display departments and the count of lecturers in each
SELECT Departments.DeptName, COUNT(Lecturers.LecturerID) AS LecturerCount
FROM Departments
LEFT JOIN Lecturers ON Departments.DeptID = Lecturers.DeptID
GROUP BY Departments.DeptName;
This groups lecturers by department and counts how many each has.
Page 5 of 5