Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Creating a Table with Composite Primary Keys
📖 Scenario: You are working on a small library database. Each book can have multiple authors, and each author can write multiple books. To keep track of which author wrote which book, you need a table that connects authors and books.
🎯 Goal: Create a table called BookAuthors that uses a composite primary key made up of BookID and AuthorID. This will ensure that each combination of book and author is unique.
📋 What You'll Learn
Create a table named BookAuthors
Include two columns: BookID and AuthorID, both integers
Set a composite primary key using both BookID and AuthorID
💡 Why This Matters
🌍 Real World
Composite primary keys are used in many-to-many relationships, like linking books and authors, students and courses, or products and orders.
💼 Career
Understanding composite primary keys is important for designing relational databases that accurately model complex relationships and maintain data integrity.
Progress0 / 4 steps
1
Create the BookAuthors table with columns
Write a SQL statement to create a table called BookAuthors with two columns: BookID and AuthorID, both of type INT.
SQL
Hint
Use CREATE TABLE followed by the table name and define the columns with their data types inside parentheses.
2
Add a composite primary key constraint
Modify the BookAuthors table creation statement to add a composite primary key constraint on both BookID and AuthorID.
SQL
Hint
Use PRIMARY KEY (BookID, AuthorID) inside the table definition to set the composite key.
3
Insert sample data into BookAuthors
Write SQL insert statements to add these exact rows into BookAuthors: (1, 101), (1, 102), and (2, 101).
SQL
Hint
Use INSERT INTO BookAuthors (BookID, AuthorID) VALUES (..., ...); for each row.
4
Try inserting a duplicate row to see the primary key in action
Write a SQL insert statement to add the row (1, 101) again into BookAuthors. This should fail because of the composite primary key.
SQL
Hint
Try inserting the same row again to see how the database prevents duplicates with the composite primary key.
Practice
(1/5)
1. What is a composite primary key in a database table?
easy
A. A primary key that uses only one column to identify rows.
B. A primary key made up of two or more columns combined to uniquely identify a row.
C. A key that allows duplicate values in the table.
D. A foreign key that references multiple tables.
Solution
Step 1: Understand primary key basics
A primary key uniquely identifies each row in a table.
Step 2: Define composite primary key
A composite primary key uses two or more columns together to ensure uniqueness.
Final Answer:
A primary key made up of two or more columns combined to uniquely identify a row. -> Option B
Quick Check:
Composite primary key = multiple columns [OK]
Hint: Composite keys combine columns to ensure unique rows [OK]
Common Mistakes:
Thinking a primary key can have duplicates
Confusing composite key with foreign key
Assuming composite key uses only one column
2. Which of the following is the correct syntax to define a composite primary key on columns order_id and product_id in SQL?
easy
A. PRIMARY KEY (order_id, product_id)
B. PRIMARY KEY order_id, product_id
C. PRIMARY KEY order_id & product_id
D. PRIMARY KEY (order_id + product_id)
Solution
Step 1: Recall SQL syntax for composite keys
Composite keys are defined by listing columns inside parentheses separated by commas.
A. Missing parentheses around the composite key columns.
B. student_id and course_id cannot be primary keys.
C. PRIMARY KEY must be declared after all columns.
D. Composite keys require UNIQUE keyword instead.
Solution
Step 1: Check syntax for composite primary key
Composite keys require parentheses around the column list in PRIMARY KEY declaration.
Step 2: Identify error in given SQL
The statement uses PRIMARY KEY student_id, course_id without parentheses, causing syntax error.
Final Answer:
Missing parentheses around the composite key columns. -> Option A
Quick Check:
Composite keys need parentheses [OK]
Hint: Always use parentheses for composite primary keys [OK]
Common Mistakes:
Omitting parentheses in PRIMARY KEY clause
Confusing primary key with unique constraint
Placing PRIMARY KEY before column definitions
5. You have a table Attendance with columns student_id, class_date, and session. You want to ensure each student can only have one attendance record per class date and session. Which composite primary key should you define?
hard
A. (class_date, session)
B. (student_id, class_date)
C. (student_id, class_date, session)
D. (student_id, session)
Solution
Step 1: Understand uniqueness requirement
Each student must have only one record per class date and session, so all three columns combined must be unique.
Step 2: Choose composite key covering all uniqueness factors
Composite key must include student_id, class_date, and session to enforce this rule.
Final Answer:
(student_id, class_date, session) -> Option C
Quick Check:
Composite key covers all uniqueness columns [OK]
Hint: Include all columns that define uniqueness in composite key [OK]