Difference Between Primary Key and Unique Key

March 2022 · 4 minute read

The main difference between primary key and unique key is that there can be only single primary key in a table and it cannot have any null value as its function is to identify each record in the table while there can be multiple unique keys in a table as their function is to ensure that a column will only have unique values such that there won’t be any duplicate data.

RDBMS stands for Relational Database Management System. It is a software that can create and manage databases. A database is a collection of tables that are related to each other. A table consists of rows and columns. A row represents the single entity while columns represent attributes. The language to manage data in an RDBMS is the Structured Query Language (SQL). There are various constraints in SQL to limit the type of data that goes to a table. These constraints help to maintain the data integrity of the data in the table. Primary key and unique key are two such constraints.

Key Areas Covered

1. What is Primary Key
     – Usage, SQL syntax
2. What is Unique Key
     – Usage, SQL syntax
3. Difference Between Primary Key and Unique Key
     – Comparison of Key Differences

Key Terms

Primary Key, Unique Key, SQL, Database, RDBMS

Difference Between Primary Key and Unique Key - Comparison Summary

What is Primary Key

Primary key helps to identify each record in the table. It does not allow any null values.

caption – SQL

In an Employee database, the employee table can have columns such as id, name, address, and salary. The most suitable column is id because each employee has a unique id. It helps to identify every entity in the table.  An example of creating a primary key is as follows.

create table employee (

            id int not null,

            name varchar (50),

            address varchar (50),

            salary decimal (10,2),

            primary key(id)

);

Difference Between Primary Key and Unique Key

It is also possible to combine two or more columns to create a primary key. Below SQL statement combines id and name to create a primary key. This combination primary key is also called the composite key.

create table employee (

            id int not null,

            name varchar (50),

            address varchar (50),

            salary decimal (10,2),

            primary key (id, name)

);

Following SQL command drops the primary key from the table:

alter table employee drop primary key;

What is Unique Key

Unique key ensures that a column will only have unique values. Therefore, there won’t be duplicate data in that column. The following SQL statement sets the name to unique. So, there cannot be two records with the same name.

create table employee (

            id int not null,

            name varchar (50) unique,

            address varchar (50),

            salary decimal (10,2),

            primary key(id)

);

Likewise, the Unique key ensures that all the values in a specific column are different.

Difference Between Primary Key and Unique Key

Definition

Primary key is a SQL constraint that allows to uniquely identify each row or record in the database table whereas unique key is an SQL constraint that does not allow the same value to be assigned to two distant rows in a database table.

Usage

Primary key helps to uniquely identify each row or record in a table while unique key avoids two records from having identical values in a column.

NULL

Primary key does not allow null value, but unique key allows only one null value.

Number of Keys per Table

A table can have only one primary key. However, there can be multiple unique keys in a table.

Conclusion

SQL constraints help to maintain the accuracy and the integrity of the data in a database. Primary Key and Unique Key are two such constraints. The basic difference between primary key and unique key is that there can be only single primary key in a table while there can be multiple unique keys in a table. A primary key automatically has a unique constraint.

Reference:

1.“SQL Constraints.” Python Relational and Logical Operators | Studytonight, Available here.
2.“SQL UNIQUE Constraint.” W3Schools Online Web Tutorials, Available here.

Image Courtesy:

1. “2394312” (CC0) via Pixabay

ncG1vNJzZmiolZm2oq2NnKamZ5Ses6ex0Z6lnJ1dl7K1w8SepWaoop66or7YZqKesV2Wu6V51Kegqq2VYrimxY4%3D