Bird
0
0

Given the table definition:

medium📝 query result Q4 of 15
SQL - Table Constraints
Given the table definition:
CREATE TABLE Enrollment (
  student_id INT,
  course_id INT,
  enrollment_date DATE,
  PRIMARY KEY (student_id, course_id)
);

What will happen if you try to insert two rows with the same student_id and course_id but different enrollment_date?
AThe second insert will overwrite the first row
BBoth inserts will succeed because enrollment_date is different
CThe database will create a duplicate primary key automatically
DThe second insert will fail due to primary key violation
Step-by-Step Solution
Solution:
  1. Step 1: Understand composite primary key uniqueness

    The combination of student_id and course_id must be unique for each row.
  2. Step 2: Analyze insert behavior

    Inserting a row with the same student_id and course_id violates the primary key constraint, regardless of enrollment_date.
  3. Final Answer:

    The second insert will fail due to primary key violation -> Option D
  4. Quick Check:

    Composite key enforces uniqueness = insert fails on duplicate [OK]
Quick Trick: Composite keys block duplicate combined values [OK]
Common Mistakes:
MISTAKES
  • Assuming different non-key columns allow duplicates
  • Thinking primary key allows overwriting rows
  • Believing database auto-creates duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes