Composite Key in DBMS
A 'combination of two or more' better describes the word 'composite'. Thus, a composite key in DBMS is a candidate key that is composed of two or more attributes and is capable of uniquely identifying a table or a relation.
Here, in this section, we will understand and discuss about the composite key as well as its role. We will also look at its working by looking at an example.
What is a Composite Key
A composite key is the DBMS key having two or more attributes that together can uniquely identify a tuple in a table. Such a key is also known as Compound Key, where each attribute creating a key is a foreign key in its own right.
Use of Composite Key
Two or more attributes together form a composite key that can uniquely identify a tuple in a table. We need to find out such table columns combination that can form a candidate key and hence a composite key.
Example of Composite Key
Below is an example to understand the working of a composite key.
Consider table A having three columns or attributes, which are as follows:
Cust_Id: A customer id is provided to each customer who visits and is stored in this field.
Order_Id: Each order placed by the customer is given an order id, which is stored in this field.
Prod_code: It holds the code value for the products available.
Prod_name: An attribute holding the name of the product on the specified product code.
Below is the table that shows the diagram for the above table:
From the table, we found that no attribute is available that alone can identify a record in the table and can become a primary key. However, the combination of some attributes can form a key and can identify a tuple in the table. In the above example, Cust_Id and Prod_code can together form a primary key because they alone are not able to identify a tuple, but together they can do so.
Points to be noted:
Note: If any type of keys used in database management is formed via two or more attributes are known as Composite keys.