Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Difference between Primary key and Unique key
Next article icon

Difference between Primary key and Unique key

Last Updated : 25 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Keys play a crucial role in relational databases by ensuring data integrity and efficient access to records. Keys help to identify individual rows in a table prevent from data being duplicated and also enable reliable relationships between tables. There are several types of Keys - Primary Key, Candidate Key, Super Key, Foreign Key, Unique Key, Alternate Key, etc. In this article, we are going to learn about the Primary Key, and the Unique Key, and the 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 be used 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 rarely. 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
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.

CREATE TABLE Student
(
Roll_number INT NOT NULL, -- Roll_number as the primary key (cannot be NULL)
Name VARCHAR(150),
Batch VARCHAR(50),
Phone_number VARCHAR(15),
Citizen_ID VARCHAR(20), -- Citizen ID can be NULL
PRIMARY KEY (Roll_number) -- Defining Roll_number as the Primary Key
);

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
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. 

CREATE TABLE Student
(
Roll_number INT NOT NULL, -- Roll_number as the primary key (cannot be NULL)
Name VARCHAR(150),
Batch VARCHAR(50),
Phone_number VARCHAR(15),
Citizen_ID VARCHAR(20) UNIQUE, -- Citizen_ID as a unique key, allows one NULL
PRIMARY KEY (Roll_number) -- Defining Roll_number as the Primary Key
);

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

ParametersPRIMARY KEYUNIQUE KEY
BasicUsed to serve as a unique identifier for each row in a table.Uniquely determines a row that isn't the primary key.
NULL value acceptanceCannot accept NULL values.Can accept NULL values.
Number of keys that can be defined in the tableOnly one primary keyMore than one unique key
IndexCreates clustered indexCreates non-clustered index
Auto IncrementA Primary key supports auto-increment value.A unique key does not support auto-increment value.
ModificationWe cannot change or delete values stored in primary keys.We can change unique key values.
UsesThe primary Key is used for indicating the rows uniquely.The Unique Key is used for preventing duplicate entries.
Syntax

CREATE TABLE Student

(

Student_Id INT PRIMARY KEY, 

Student_name VARCHAR(150), 

roll_number INT(10)

)

CREATE TABLE House

(

House_Number INT UNIQUE, 

House_Name VARCHAR(150), 

House_Address 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.

Next Article
Difference between Primary key and Unique key

A

Ankit_Bisht
Improve
Article Tags :
  • Technical Scripter
  • DBMS
  • Difference Between
  • DBMS-Relational Algebra
  • DBMS-SQL

Similar Reads

    Difference between Primary key and Super key
    Key concepts that play a significant role in DBMS are the Primary Key and the Super Key. While both are fundamental to the structure and functionality of a database, they serve distinct purposes and have different characteristics. A Primary Key is a specific, minimal set of attributes that uniquely
    4 min read
    Difference between Primary Key and Foreign Key
    Keys are one of the most important elements in a relational database to maintain the relationship between the tables and it also helps in uniquely identifying the data from a table. The primary key is a key that helps uniquely identify the tuple of the database. In contrast, the Foreign Key is a key
    4 min read
    Difference between Primary and Candidate Key
    In relational database management systems(RDBMS) both the Primary Key and Candidate Key are the essential components and are used to uniquely identify records (tuples) within a table. They both are fundamental concepts used to ensure data integrity and prevent duplication of data. These(Primary key
    6 min read
    Difference between Primary and Secondary Memory
    Primary memory is used to store actively used data and is very fast but loses information when power is lost. Secondary memory provides long-term storage for files and programs, retaining data safely but is slower than primary memory. In this article, we are going to discuss the difference between p
    5 min read
    Difference between Super Key and Candidate Key
    Tables in databases need ways to identify each record uniquely. Super keys and candidate keys help with this. They're special columns or groups of columns that can tell records apart. Both super keys and candidate keys can have empty spaces (null values). These keys also help connect different table
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences