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.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.