Composite Key in DBMS

Composite Key

In this article, we will learn about Super Key in DBMS.

Composite key is a candidate key, which is combination of two or more attributes of a table to uniquely identify occurrence an entity.

composite key in DBMS

Composite Key in DBMS

A Composite Key is a combination of one or more attributes. If a single column alone fails to be served as a primary key then combination  columns would help to uniquely access a record from table such type of keys or nothing but composite keys.

composite Key in DBMS

Example

If a table contains three columns [name, address, course] individually feel to access the record uniquely combination of either [name, course],[name, address],[course, address] would help to access records uniquely
  • Consider a table  with three  attitudes customer ID, product ID, product quantity
  • Customer ID needs to be entered for each time the customer purchases an  order hence customer ID appears more than once in the customer ID table hence it cannot be served as the primary key i.e it failed to uniquely identify a record
  • Example customer ID 66 has placed two orders hence customer ID appeared 66 two times in the customer ID column
customer ID product ID product quantity
66 9023 10
67 9023 15
68 9031 20
69 9031 18
66 9111 50
  • Product  ID and product quality cannot be declared as the primary key because of more than one customer purchase same product and the same quantity
  • In this situation all three attributes fail  to serve as a primary key .Hence  combination of these  attribute can be used as a primary key
  • For example [customer ID, product ID] can be used as the primary key table customers this combination helps to uniquely access records of customer.
Create table ORDER
(
    Customer_ID int ,
    Product_ID int ,
    Order_Quantity int ,
    Primary key (Customer_ID, Product_ID)
)
  • While choosing a set of attributes for a primary key, we always choose the minimal set that has a minimum number of attributes.
  • For example, if there are two sets that can identify a row in the table, the set that has a minimum number of attributes should be chosen as the primary key.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription