Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 20

Composite Key in MySQL

In this lesson: What a composite key is, when to use it and how to create it in MySQL — examples with output and errors.

What is a composite key

A composite key is a primary key made of two or more columns together. It is used when no single column uniquely identifies a row, but the combination does. For example, one student can enrol in many courses, so neither student_id nor course_id alone is unique — but together they are.

💡 Composite key = several columns acting as one primary key, unique as a combination.

Why you need one

Consider an enrolments table linking students to courses. The same student appears in many rows (many courses), and the same course appears in many rows (many students). No single column is unique. But the pair (student_id, course_id) should be unique — a student should not enrol in the same course twice.

Creating a composite key

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    grade CHAR(1),
    PRIMARY KEY (student_id, course_id)
);

Listing both columns in one PRIMARY KEY (...) clause makes them a composite key. The combination must be unique, though each column alone may repeat.

How it enforces uniqueness

INSERT INTO enrollments VALUES (1, 101, 'A');   -- ok
INSERT INTO enrollments VALUES (1, 102, 'B');   -- ok (same student, new course)
INSERT INTO enrollments VALUES (1, 101, 'C');   -- ERROR: duplicate pair

Student 1 can take courses 101 and 102, but cannot enrol in course 101 twice — the composite key blocks the duplicate combination.

With foreign keys

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    PRIMARY KEY (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

In a link table, each part of the composite key is often also a foreign key. Here the two columns reference the students and courses tables, connecting them while keeping each pair unique.

Composite vs single key

PointSingle keyComposite key
ColumnsOneTwo or more
Unique onThat columnThe combination
Common useNormal tablesLink tables (M:N)
Individual repeatsNot allowedAllowed if pair differs

Common mistakes

  • Expecting single columns to be unique. Only the combination is unique.
  • Using two PRIMARY KEY clauses. List all columns in one clause instead.
  • Order confusion. The columns form one key; their order can affect indexing.
  • Using a composite key when a single id would do. Only use it when needed.

Summary

  • A composite key is a primary key of two or more columns.
  • Use it when only the combination of columns is unique.
  • Create it with one PRIMARY KEY (col1, col2) clause.
  • Individual columns may repeat if the whole combination does not.
  • It is common in link tables, where columns are also foreign keys.

Next, learn all key types in keys in DBMS explained, or auto ids in AUTO_INCREMENT.

Frequently Asked Questions

What is a composite key in MySQL?
A composite key is a primary key made of two or more columns together. It is used when no single column is unique on its own, but the combination of columns is. For example, (student_id, course_id) together can uniquely identify one enrolment.
When should I use a composite key?
Use a composite key when a single column cannot uniquely identify a row, but a combination can. This is common in link tables for many-to-many relationships, like enrollments where the same student and same course pair should appear only once.
How do I create a composite key in MySQL?
List all the key columns in one PRIMARY KEY clause: PRIMARY KEY (student_id, course_id). MySQL then treats the combination as the unique identifier, allowing the same value in one column as long as the pair is unique.
What is the difference between a composite key and a single primary key?
A single primary key uses one column to identify rows. A composite key uses several columns together. The composite key is unique as a combination, so individual columns may repeat as long as the whole combination does not.
Can a composite key be used with foreign keys?
Yes. Each column of a composite key can also be a foreign key to another table. In a link table, the two columns often reference the primary keys of the two tables being connected.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 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.
Next topicReferential Integrity in DBMS Your progress: 20 / 151 (13%) · Lesson 21 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free