🧩 Composite Key (Multi-Column Key)

🟡 Intermediate

📖 Definition

A Composite Key is a key consisting of two or more columns combined together to uniquely identify a record in a table. Composite keys are essential when no single column alone is unique, but the combination of multiple columns guarantees uniqueness.

🌍 Multilingual Explanations

English

A Composite Key uses multiple columns together as a Primary Key. It is heavily used in junction/bridge tables for Many-to-Many relationships.

Hindi

Jab ek akela column row ko unique na bana sake, tab do ya zyada columns ko mila kar Composite Key banayi jaati hai.

Marathi

Jeva ek column unique nasto, teva multiple columns ektra aaun Composite Key banavli jaate.

Hinglish

Composite Key = Multi-column Primary Key. E.g. in `order_items`, `(order_id, product_id)` together form a Composite Primary Key.

🧱 Real-World Use Case: Junction Tables

In a Many-to-Many relationship between orders and products, an order can contain multiple products, and a product can belong to multiple orders. The bridge table order_items uses a Composite Primary Key:

order_id (FK 1) product_id (FK 2) quantity Composite Key Status
1001 501 2 ✅ Allowed (Order 1001, Product 501)
1001 502 1 ✅ Allowed (Order 1001, Product 502)
1002 501 5 ✅ Allowed (Order 1002, Product 501)
1001 501 3 ❌ Rejected (Duplicate composite key combination!)

⚡ SQL Syntax

-- Defining a Composite Primary Key at table level
CREATE TABLE order_items (
    order_id INT REFERENCES orders(order_id) ON DELETE CASCADE,
    product_id INT REFERENCES products(product_id) ON DELETE CASCADE,
    quantity INT NOT NULL CHECK (quantity > 0),
    unit_price DECIMAL(10, 2) NOT NULL,
    CONSTRAINT pk_order_items PRIMARY KEY (order_id, product_id)  -- Composite Primary Key
);

💡 Production Example

-- University Student Course Enrollment Table
CREATE TABLE student_enrollments (
    student_id INT NOT NULL,
    course_id INT NOT NULL,
    semester VARCHAR(10) NOT NULL,
    enrollment_date DATE DEFAULT CURRENT_DATE,
    grade CHAR(2),
    CONSTRAINT pk_student_enrollments PRIMARY KEY (student_id, course_id, semester)
);

-- Querying with Composite Primary Key filter
SELECT * FROM student_enrollments
WHERE student_id = 202401 AND course_id = 101 AND semester = 'FALL2026';

⚠️ Common Pitfalls & Mistakes

  • Attempting column-level Composite Key declaration: You CANNOT declare `PRIMARY KEY` on multiple individual columns in column definition list. Composite Primary Keys MUST be declared at table level: `PRIMARY KEY (col1, col2)`.
  • Over-expanding Composite Keys: Using 4 or 5 columns in a Composite Key makes Foreign Key referencing complex. In such cases, consider using a single Surrogate Key with a UNIQUE multi-column constraint instead.

🎯 Tiered Practice Exercises

  1. Beginner: Create an order_items table with a composite primary key on order_id and product_id.
  2. Intermediate: Insert valid rows into order_items and demonstrate that duplicating the combined pair is blocked.
  3. Advanced: Create a child table that references a parent table's Composite Primary Key using a multi-column Foreign Key constraint.

🧭 Navigation

← Previous: Alternate Key | Keys Overview | Next: Surrogate Key →