HomeMYSQLWhat is a composite key?

What is a composite key?

A composite key in a database is a primary key that consists of two or more columns in a table. Instead of using a single column to uniquely identify a record, the combination of multiple columns together ensures uniqueness.

Key Points:

  1. Purpose: Ensures that each row in a table is unique when a single column is not sufficient.
  2. Composition: Made up of two or more attributes (columns).
  3. Uniqueness: The combination of all columns in the composite key must be unique for every row. Individually, the columns may contain duplicate values.
  4. Use case: Common in many-to-many relationship tables (junction tables).

Example:

Consider a table CourseEnrollment:

student_idcourse_idenrollment_date
11012025-08-01
11022025-08-02
21012025-08-01
  • Here, neither student_id nor course_id alone can uniquely identify a row, because a student can enroll in multiple courses and a course can have multiple students.
  • Composite key: (student_id, course_id) ensures each row is unique.

So, a composite key is all about uniqueness through combination, not a single column.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *