Introduction
A composite primary key uses more than one column to uniquely identify each row in a table. It helps when one column alone can't guarantee uniqueness.
Jump into concepts and practice - no test required
CREATE TABLE table_name ( column1 datatype, column2 datatype, ..., PRIMARY KEY (column1, column2) );
CREATE TABLE Enrollment ( student_id INT, course_id INT, enrollment_date DATE, PRIMARY KEY (student_id, course_id) );
CREATE TABLE Attendance ( employee_id INT, attendance_date DATE, status VARCHAR(10), PRIMARY KEY (employee_id, attendance_date) );
CREATE TABLE Orders ( order_id INT, product_id INT, quantity INT, PRIMARY KEY (order_id, product_id) );
CREATE TABLE LibraryLoans ( member_id INT, book_id INT, loan_date DATE, PRIMARY KEY (member_id, book_id) ); INSERT INTO LibraryLoans (member_id, book_id, loan_date) VALUES (1, 101, '2024-06-01'), (1, 102, '2024-06-02'), (2, 101, '2024-06-03'); SELECT * FROM LibraryLoans;
order_id and product_id in SQL?OrderDetails with composite primary key (order_id, product_id), what will this query return?SELECT * FROM OrderDetails WHERE order_id = 101;
CREATE TABLE Enrollment (
student_id INT,
course_id INT,
PRIMARY KEY student_id, course_id
);
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?