0
0
SQLquery~5 mins

Composite primary keys in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a composite primary key in a database?
A composite primary key is a primary key made up of two or more columns that together uniquely identify each row in a table.
Click to reveal answer
beginner
Why would you use a composite primary key instead of a single column primary key?
You use a composite primary key when no single column can uniquely identify a row, but a combination of columns can.
Click to reveal answer
beginner
How do you define a composite primary key in SQL?
You list multiple columns inside the PRIMARY KEY clause, separated by commas, like: PRIMARY KEY (column1, column2).
Click to reveal answer
intermediate
Can a composite primary key contain NULL values in any of its columns?
No, all columns in a composite primary key must have NOT NULL values to ensure uniqueness.
Click to reveal answer
beginner
Example: What is the composite primary key in this table definition? CREATE TABLE Enrollment ( student_id INT, course_id INT, enrollment_date DATE, PRIMARY KEY (student_id, course_id) );
The composite primary key is the combination of student_id and course_id columns. Together, they uniquely identify each enrollment record.
Click to reveal answer
What does a composite primary key consist of?
AMultiple columns combined to uniquely identify a row
BA single column with multiple values
CA foreign key referencing another table
DA column that allows NULL values
Which SQL syntax correctly defines a composite primary key?
APRIMARY KEY column1, column2
BPRIMARY KEY (column1, column2)
CPRIMARY KEY column1 AND column2
DPRIMARY KEY column1 OR column2
Can any column in a composite primary key contain NULL values?
ANo, all columns must be NOT NULL
BOnly one column can have NULL
CIt depends on the database
DYes, NULLs are allowed
Why might you choose a composite primary key?
ATo allow duplicate rows
BTo allow NULL values in keys
CTo speed up queries
DBecause no single column uniquely identifies rows
In the table Enrollment(student_id, course_id), which is the best primary key?
Astudent_id only
Bcourse_id only
Cstudent_id and course_id together
Denrollment_date
Explain what a composite primary key is and why it is useful.
Think about how two or more columns together can identify a record uniquely.
You got /4 concepts.
    Describe how to define a composite primary key in SQL with an example.
    Remember the parentheses and commas between column names.
    You got /3 concepts.