MySQL + SQL · Lesson 19
Composite Key and Unique Key
What is a Composite Key?
A composite key is a primary key made of TWO OR MORE columns together. Used when no single column is unique on its own.
Example
CREATE TABLE marks (
roll_no INT,
subject VARCHAR(20),
score INT,
PRIMARY KEY (roll_no, subject)
);Neither roll_no nor subject is unique alone, but together they uniquely identify a row.
Summary
- A composite key combines multiple columns as the primary key.
- Used when one column alone cannot uniquely identify a row.