Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Engineering Mathematics Discrete Mathematics Digital Logic and Design Computer Organization and A

Difference between Primary key and Unique key


Ankit_Bisht

Read Discuss

Prerequisite – Keys in Relational Model

Keys have a crucial role in Relational Algebra in DBMS. There are several types of
Keys like Primary Key, Candidate Key, Super Key, Foreign Key, Unique Key,
Alternate Key, etc. In this article, basically, we are going to see the Primary Key,
Unique Key, and Key Differences between them.

Primary Key
A primary key is a column of a table that uniquely identifies each tuple (row) in that
table. The primary key enforces integrity constraints to the table. Only one primary
key is allowed to use in a table. The primary key does not accept any duplicate and
NULL values. The primary key value in a table changes very rarely so it is chosen
with care where the changes can occur in a seldom manner. A primary key of one
table can be referenced by the foreign key of another table.

For a better understanding of the primary key, we take a table named Student
table, having attributes such as Roll_number, Name, Batch, Phone_number, and
Citizen_ID.

Primary Key

The roll number attribute can never have an identical and NULL value, because
every student enrolled in a university can have a unique roll number, therefore two
students cannot have the same roll number, and each row in a table is uniquely
identified with the student’s roll number. So, we can make Roll_number attribute as
a primary key in this case.

Features of Primary Key

Some of the essential features of Primary Keys are discussed below.

There will be no duplicate row in case of a Primary Key.


Only a single primary key exists for a table.
Primary Key contains NOT NULL constraints.
The primary Key can be made from one or more table fields.

Unique Key
Unique Key constraints also identify an individual tuple uniquely in a relation or
table. A table can have more than one unique key, unlike a primary key. Unique key
constraints can accept only one NULL value for the column. Unique constraints are
also referenced by the foreign key of another table. It can be used when someone
wants to enforce unique constraints on a column and a group of columns which is
not a primary key.

For a better understanding of the unique key, we take the Student table with
Roll_number, Name, Batch, Phone_number, and Citizen_ID attributes.

Unique Key

Roll number attribute is already assigned with the primary key and Citizen_ID can
have unique constraints where each entry in a Citizen_ID column should be unique
because each citizen of a country must have his or her Unique identification number
like an Aadhaar Number. But if the student is migrated to another country in that
case, he or she would not have any Citizen_ID and the entry could have a NULL
value as only one NULL is allowed in the unique constraint.
Features of Unique Key

Some of the essential features of Unique Keys are discussed below.

There can be more than one unique key for a table.


Unique Keys have the liberty of having NULL values in the column.
Unique Keys can be formed from one or more tables.
Foreign Keys can refer to Unique Keys for referencing.

Differences between Primary Key and Unique Key

Parameters PRIMARY KEY UNIQUE KEY

Used to serve as a unique


Uniquely determines a row
Basic identifier for each row in a
that isn’t the primary key.
table.

NULL value
Cannot accept NULL values. Can accept NULL values.
acceptance

Number of keys that


can be defined in the Only one primary key More than one unique key
table

Creates non-clustered
Index Creates clustered index
index

A unique key does not


A Primary key supports
Auto Increment support auto-increment
auto-increment value.
value.

We cannot change or delete


We can change unique key
Modification values stored in primary
values.
keys.

Uses The primary Key is used for The Unique Key is used for
indicating the rows preventing duplicate
Parameters PRIMARY KEY UNIQUE KEY

uniquely. entries.

CREATE TABLE House


CREATE TABLE Student
(
(
House_Number INT
Student_Id INT PRIMARY
UNIQUE,
KEY,
Syntax House_Name
Student_name
VARCHAR(150),
VARCHAR(150),
House_Address
roll_number INT(10)
VARCHAR(250)
)
)

Conclusion

The primary key will not accept NULL values whereas the Unique key can accept
NULL values.
A table can have only one primary key whereas there can be multiple unique
keys on a table.
A Clustered index is automatically created when a primary key is defined
whereas a Unique key generates the non-clustered index.
A Primary Key can be a Unique Key, but a Unique Key cannot be a primary key.
Last Updated : 17 May, 2023 100

Similar Reads
1. Difference between Primary Key and Foreign Key

2. Difference between Primary key and Super key

3. Difference between Primary and Candidate Key

4. Primary key in MS SQL Server


5. Primary key constraint in SQL

6. Difference between Primary and Secondary Memory

7. Difference between Primary and Secondary Data

8. Difference Between Primary Cell and Secondary Cell

9. Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and


Foreign)

10. Difference between Private key and Public key

Previous Next

Article Contributed By :

Ankit_Bisht
Ankit_Bisht

Vote for difficulty


Current difficulty : Medium

Easy Normal Medium Hard Expert

Improved By : itskawal2000, anshitaagarwal1609, anikaseth98, junedknw123, kamalsagar,


raj2002, adityaarxehn

Article Tags : DBMS-Relational Algebra, DBMS-SQL, DBMS, Difference Between,


Technical Scripter

Practice Tags : DBMS

Improve Article Report Issue


A-143, 9th Floor, Sovereign Corporate
Tower, Sector-136, Noida, Uttar Pradesh -
201305

feedback@geeksforgeeks.org

Company Explore

About Us Job Fair For Students

Legal POTD: Revamped

Careers Python Backend LIVE

In Media Android App Development

Contact Us DevOps LIVE

Advertise with us DSA in JavaScript

Languages Data Structures

Python Array

Java String

C++ Linked List

PHP Stack

GoLang Queue

SQL Tree

R Language Graph

Android Tutorial

Algorithms Web Development

Sorting HTML

Searching CSS

Greedy JavaScript

Dynamic Programming Bootstrap

Pattern Searching ReactJS

Recursion AngularJS
Backtracking NodeJS

Computer Science Python

GATE CS Notes Python Programming Examples

Operating Systems Django Tutorial

Computer Network Python Projects

Database Management System Python Tkinter

Software Engineering OpenCV Python Tutorial

Digital Logic Design Python Interview Question

Engineering Maths

Data Science & ML DevOps

Data Science With Python Git

Data Science For Beginner AWS

Machine Learning Tutorial Docker

Maths For Machine Learning Kubernetes

Pandas Tutorial Azure

NumPy Tutorial GCP

NLP Tutorial

Deep Learning Tutorial

Competitive Programming System Design

Top DSA for CP What is System Design

Top 50 Tree Problems Monolithic and Distributed SD

Top 50 Graph Problems Scalability in SD

Top 50 Array Problems Databases in SD

Top 50 String Problems High Level Design or HLD

Top 50 DP Problems Low Level Design or LLD

Top 15 Websites for CP Top SD Interview Questions

Interview Corner GfG School

Company Preparation CBSE Notes for Class 8

Preparation for SDE CBSE Notes for Class 9

Company Interview Corner CBSE Notes for Class 10

Experienced Interview CBSE Notes for Class 11


Internship Interview CBSE Notes for Class 12

Competitive Programming English Grammar

Aptitude

Commerce UPSC

Accountancy Polity Notes

Business Studies Geography Notes

Microeconomics History Notes

Macroeconomics Science and Technology Notes

Statistics for Economics Economics Notes

Indian Economic Development Important Topics in Ethics

Income Tax UPSC Previous Year Papers

Management

SSC/ BANKING Write & Earn

SSC CGL Syllabus Write an Article

SBI PO Syllabus Improve an Article

SBI Clerk Syllabus Pick Topics to Write

IBPS PO Syllabus Write Interview Experience

IBPS Clerk Syllabus Internships

Aptitude Questions Video Internship

SSC CGL Practice Papers

@geeksforgeeks , Some rights reserved

You might also like