Scenario: Database Design Challenge
Background: You are a database administrator working for a fictional university, and the university
wants to manage information about courses, students, and their enrollment in courses. Your task is
to design a database to store this information efficiently.
Requirements:
1. Create two tables: one for storing information about courses and another for storing
information about students.
2. Each table should have its own primary key.
3. Implement necessary constraints to ensure data integrity.
Table 1: Courses
Fields:
CourseID (Primary Key)
CourseName
Instructor
Credits (should not be negative)
Table 2: Students
Fields:
StudentID (Primary Key)
FirstName
LastName
Email (should be unique)
DateOfBirth (should not be in the future)
Additional Constraints:
1. The CourseID in the Courses table should be a 4-digit number.
2. The StudentID in the Students table should be a 5-digit number.
3. The Email in the Students table should follow a standard email format (e.g.,
[email protected]).
4. Ensure that the enrollment of students in courses is recorded in a separate table, called
Enrollments.
5. The Enrollments table should have the following fields:
EnrollmentID (Primary Key)
StudentID (Foreign Key referencing Students table)
CourseID (Foreign Key referencing Courses table)
EnrollmentDate (should not be in the future)
Case Study Tasks:
1. Update the email address of a student.
2. Retrieve courses with more than 3 credits or instructed by Prof. Smith.
Step 1: Make Tables
-- Create Courses table
CREATE TABLE Courses (
CourseID INT PRIMARY KEY CHECK (CourseID >= 1000 AND CourseID <= 9999),
CourseName VARCHAR(255),
Instructor VARCHAR(255),
Credits INT CHECK (Credits >= 0)
);
-- Create Students table
CREATE TABLE Students (
StudentID INT PRIMARY KEY CHECK (StudentID >= 10000 AND StudentID <= 99999),
FirstName VARCHAR(255),
LastName VARCHAR(255),
Email VARCHAR(255) UNIQUE CHECK (Email LIKE '%@%.%'),
DateOfBirth DATE CHECK (DateOfBirth <= CURRENT_DATE)
);
-- Create Enrollments table
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE CHECK (EnrollmentDate <= CURRENT_DATE),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
-- Insert data into Courses table
INSERT INTO Courses (CourseID, CourseName, Instructor, Credits) VALUES
(1001, 'Introduction to Computer Science', 'Prof. Smith', 3),
(1002, 'Database Management', 'Prof. Johnson', 4),
(1003, 'Web Development', 'Prof. Williams', 3);
-- Insert data into Students table
INSERT INTO Students (StudentID, FirstName, LastName, Email, DateOfBirth) VALUES
(10000, 'John', 'Doe', '
[email protected]', '1998-05-15'),
(10001, 'Jane', 'Smith', '
[email protected]', '1997-08-20'),
(10002, 'Bob', 'Johnson', '
[email protected]', '1999-02-10');
-- Insert data into Enrollments table
INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, EnrollmentDate) VALUES
(1, 10000, 1001, '2023-01-15'),
(2, 10001, 1002, '2023-02-20'),
(3, 10002, 1003, '2023-03-10');
-- Query 1: Update the email address of a student
UPDATE Students
SET Email = '
[email protected]'
WHERE StudentID = 10000;
-- Query 5: Retrieve courses with more than 3 credits or instructed by Prof. Smith
SELECT CourseName, Credits, Instructor
FROM Courses
WHERE Credits > 3 OR Instructor = 'Prof. Smith'
ORDER BY Credits DESC;