Focus: Spot problems in database designs
Time: 15-20 minutes
Hint: Think about what makes each student unique.
Missing PRIMARY KEY
The student_id should be marked as the PRIMARY KEY.
Why it matters: Without a primary key, you could have duplicate student_ids, and other tables can't reliably reference students.
Correct design:
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
major VARCHAR(50)
);
Hint: What happens when you try to calculate total prices?
Price is stored as VARCHAR (text) instead of DECIMAL
Why this is bad:
Correct design:
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10,2), -- Fixed!
stock INT
);
Think about: What if Alice wants to take 4 courses? What if you need to find all students in Math?
Correct design - Use separate tables:
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100)
);
CREATE TABLE Enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);
Now: Students can take any number of courses, and it's easy to query who's in each course!