×
>
<

DBMS

DBMS Key Composite | CrackEase

Composite Key in DBMS

Composite Key

A composite key is a combination of two or more attributes (columns) that together uniquely identify a row in a table.

Note: Any key (super key, primary key, candidate key, etc.) becomes a composite key if it consists of more than one attribute.

If a single column cannot uniquely identify a record, a combination of columns can be used — that combination is the composite key.

Examples

Example: If a table contains three columns [name, address, course], a single column may not uniquely identify a record. Combinations like [name, course], [name, address] or [course, address] might uniquely identify rows.

  • Consider a table with three attributes: Customer_ID, Product_ID, Order_Quantity.
  • Customer_ID may repeat (a customer can place multiple orders), so it cannot serve as a primary key alone.
  • For example, Customer_ID 66 appears twice because that customer placed more than one order.

Customer_IDProduct_IDOrder_Quantity
66902310
67902315
68903120
69903118
66911150

  • Neither Product_ID nor Order_Quantity alone can uniquely identify a row.
  • However, the combination [Customer_ID, Product_ID] uniquely identifies each order line — this combination can be declared as a composite primary key.

  
CREATE TABLE ORDERS
(
    Customer_ID   INT,
    Product_ID    INT,
    Order_Quantity INT,
    PRIMARY KEY (Customer_ID, Product_ID)
);
  

  • When choosing attributes for a composite primary key, prefer the minimal set of columns that uniquely identify a row.
  • If two different sets can both uniquely identify rows, choose the set with fewer attributes (the minimal set).

Footer Content | CrackEase