What is the Composite Primary Key?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

We can break the term "composite primary key" into two components composite + primary keycomposite\ +\ primary \ key.

  • Composite:
    means a combination of multiple components.
  • primary key:
    means a key that can uniquely identify the rows in a table. Also, the key cannot be null.

Therefore by combining the above definitions, we can say that a composite primary key is a primary key formed by combining one or more keys (columns) from the table. These keys may or may not guarantee to the identification of the rows of the table individually. When combined, they become a primary key and can uniquely identify a row.

How to Create Composite Primary Key?

Creating Composite Primary Key While Table Creation

Syntax to create a composite key in SQL:

As shown in the syntax, we need to determine at least two columns that don't accept NULL values for defining a composite key. The remaining columns of the table then follow. At last, we can write a constraint to define the composite primary key.

Example:

Creating a database:

Using database:

Creating a table with a composite key:

In the example given below, we have made the composite key the combination of two columns i.e. mobile number and username because all the rows of the table student can be uniquely identified by this composite key.

Inserting records in the table:

Querying the records:

Output :

mail_idpasswordusernamecoursemobile
[email protected]iloveedtechAsxnaSystem Design9999988888
[email protected]Prateek123prateek27Data Structure9999888827
[email protected]stat1st1cgodprag9XData Science9988887777
[email protected]anshu2019anshu26Big Data9999988888
[email protected]1E1F1Pnav08Product management9955663322

Adding Composite Primary Key in Existing Table

To add a composite primary key in the existing table we can use the following syntax:

If a primary key already exists we can use the following syntax:

In the above example, we can change the composite primary key as a combination of mail_idmail\_id and passwordpassword as:

Querying the records:

Output:

mail_idpasswordusernamecoursemobile
[email protected]iloveedtechAsxnaSystem Design9999988888
[email protected]Prateek123prateek27Data Structure9999888827
[email protected]stat1st1cgodprag9XData Science9988887777
[email protected]anshu2019anshu26Big Data9999988888
[email protected]1E1F1Pnav08Product management9955663322

Composite Primary Key Rules

The following rules should be kept in mind while creating a composite primary key:

composite-primary-key-rules

  • The combination can make a composite primary key of two or more candidate keys.
  • A composite key cannot be null.
  • A composite Key cannot contain duplicates.
  • We can have duplicate values in an individual column, but they must be unique across the columns.
  • Null values are not allowed in any columns in the composite primary key.

Learn More

To learn more about primary keys and other relevant articles on Scaler Topics

Conclusion

  • Composite primary key combines two or more columns to form a primary key for the table.
  • To add a composite primary key to an existing table, we can use the ALTERALTER command.
  • A composite key cannot be null.
  • A composite Key cannot contain duplicates.
  • We can have duplicate values in an individual column, but they must be unique across the columns.
  • Null values are not allowed in any columns in the composite primary key.