Difference Between Primary Key and Unique 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

Overview

Keys in Relational Database Management System(RDBMS) or SQL are the columns that help us to identify the relationship between two or more two tables. Keys are used for accessing data from the tables. Primary keys are used to identify each record(not duplicate) in the table, whereas a unique Key constraint is a column or combination of columns in a table that uniquely identifies a record. In this article by Scaler Topics, we will learn about Difference Between Primary Key and Unique Key.

Primary Key in DBMS

Primary Key (non-null key) is a column of the table that identifies every record that should be unique in the table. The primary Key column should not contain any duplicate values(every column value in the primary key column should be unique), therefore there should only be one primary key column in the table. Primary keys are generally used as Foreign keys in the other table.

Reasons to Use Primary Key

  • According to the definition of Primary Key, the primary key column should contain unique values, so the primary key is used to identify every record in the table.
  • We can use the Primary key when we don't want any duplicate or NULL values in the particular column.
  • Data is organized in a sequence of clustered indexes whenever you physically organize the DBMS table.

Features of Primary Key

  • Values in the primary key column are NOT NULL.
  • We can create only one primary key for a table.
  • Primary Key column should not contain duplicate values.
  • It implements entity integration of the table.

Unique Key in DBMS

Unique Key constraint is a column or combination of columns in a table that uniquely identifies a record. Unique constraints are also referenced by the foreign key of another table. Unique Key is the same as the primary key, but the Unique key column can store NULL values in it.

Reasons to Use Unique Key

  • Unique key can be used when you have to keep null values in the column.
  • Unique key is to make sure that the information in the column for each table record is unique.
  • Unique Key allows us to enter the NULL values in it.

Features of Unique Key

  • We can construct the unique key from one or more table fields.
  • We can define more than one unique key in the table.
  • A unique key can be easily referenced by a foreign key.
  • We can store NULL values in the Unique Key, but only one NULL is allowed null per column.

Primary Key vs Unique Key: Key Differences

Purpose:

  • Primary Key: Ensures each record in a table is unique and is used to identify each row uniquely within the table. It also serves as a reference for foreign keys in related tables.
  • Unique Key: Ensures that each value in a column or set of columns is unique but may not be used for identifying rows or as a reference for foreign keys.

Number of Occurrences:

  • Primary Key: Only one primary key is allowed per table, and it must contain unique values for all rows.
  • Unique Key: Multiple unique keys can exist within a table, each enforcing uniqueness on different columns or combinations of columns.

Null Values:

  • Primary Key: Does not allow NULL values in the key columns, meaning every row must have a value in the primary key column(s).
  • Unique Key: Allows NULL values in the key columns, as long as the non-NULL values remain unique.

Syntax:

  • Primary Key:
  • Unique Key:

Example:

  • Primary Key:
  • Unique Key:

Difference Between Primary Key and Unique Key: Comparison Chart

CharacteristicPrimary KeyUnique Key
PurposeUniquely identifies each record in a table.Enforces the uniqueness of values in a column or set of columns, but does not necessarily identify each record.
Number of KeysOnly one Primary Key per table.Multiple Unique Keys can exist in a table.
Null ValuesDoes not allow NULL values.Allows one NULL value per column in the unique key.
IndexingAutomatically indexed by the database for efficient retrieval.May or may not be automatically indexed, depending on the database management system.
Clustered Index (SQL)In some database systems, the Primary Key may automatically create a clustered index, which determines the physical order of data rows in the table.Unique Key does not typically create a clustered index.
Foreign Key RelationshipCan be used as a reference for foreign keys in other tables to establish relationships.Can also be used as a reference for foreign keys, but it may allow multiple records with NULL values in the referenced column(s).
ChangeabilityChanging the value of a Primary Key is discouraged, as it would require updating all related foreign keys.Changing the value of a Unique Key is possible and generally does not affect related records.
Data IntegrityEnforces both uniqueness and entity integrity.Enforces only uniqueness.

Example of Primary Key

The following example describes that there are database sales, and there are two tables in that database:

  1. Customers
  2. Item

Customers Tables have five attributes:

  1. Customer ID
  2. Customer Name
  3. Phone Number
  4. Email Address
  5. Item Id

Item Tables have four attributes:

  1. Item ID
  2. Item Name
  3. Item Price
  4. Items Left

In Customer Table, Customer Id can never contain a duplicate or null value because when the new customer data is entered, it will generate a new customer_id for that customer. So, the customer_id column contains unique values. So can be considered as a primary key.

Customer Table

In Items Table, Item_Id never contains a duplicate or null value because every item has its unique item id. You can easily identify each row of a table with item id. So it is considered as a primary key.

Items Table

Example of Unique Key

The following example describes that there is a table called Students, in that Students table there are 6 attributes:

  1. Student_ID
  2. Student_Name
  3. Phone Number
  4. Roll Number
  5. Batch Number
  6. Email Address

In this Student Table, Student_Id can have unique constraints where entries are in the column. So, we can consider Student_ID as a Unique Key.

Student Tables

Conclusion

  • Primary keys enforce both uniqueness and non-null values, while unique keys only enforce uniqueness.
  • Primary keys are typically single columns or combinations, whereas unique keys can apply to single or multiple columns.
  • Primary keys are table-level constraints, whereas unique keys are column-level constraints.
  • Primary keys often create clustered indexes, impacting data storage, whereas unique keys may or may not result in clustered indexes.
  • Primary keys are commonly used in establishing relationships through foreign keys, ensuring referential integrity. Unique keys can serve a similar purpose but without the non-null constraint.
  • A table can have multiple unique key constraints on different columns, providing more flexibility in data integrity.
  • Primary keys are ideal for tables requiring a unique identifier for each row, such as customer IDs or product codes.
  • Unique keys are suitable when enforcing uniqueness for attributes like email addresses in a user database, where not every user may have an email.