SQL UNIQUE vs PRIMARY KEY: What's the Difference? Practical Examples in Oracle SQL
When creating a database table, you often need to make sure that duplicate records are not inserted.
For example, think about an employee database.
Every employee has an employee ID and an email address. The employee ID should be different for every employee, and two employees should not share the same email address.
How do you enforce these rules in SQL?
Two commonly used SQL constraints can help:
- PRIMARY KEY
- UNIQUE
Both prevent duplicate values, but they are not exactly the same.
Let's understand the difference with a practical Oracle SQL example.
- Create an Employee Table
Suppose you are working on an employee management application.
The employees table already contains the following records:
employee_id| employee_name| email 101| Rahul| rahul@gmail.com 102| Priya| priya@gmail.com 103| Amit| amit@gmail.com
Your manager gives you two requirements:
- Every employee must have a different employee ID.
- No two employees should have the same email address.
You can use PRIMARY KEY and UNIQUE constraints to meet these requirements.
Let's create the table.
CREATE TABLE employees ( employee_id NUMBER PRIMARY KEY, employee_name VARCHAR2(50), email VARCHAR2(100) UNIQUE );
Here:
- "employee_id" uses PRIMARY KEY.
- "email" uses UNIQUE.
- "employee_name" stores the employee's name.
- Insert Employee Records
Now insert three employees into the table.
INSERT INTO employees VALUES (101, 'Rahul', 'rahul@gmail.com');
INSERT INTO employees VALUES (102, 'Priya', 'priya@gmail.com');
INSERT INTO employees VALUES (103, 'Amit', 'amit@gmail.com');
All three INSERT statements execute successfully.
You can check the records using:
SELECT * FROM employees;
Expected output:
EMPLOYEE_ID| EMPLOYEE_NAME| EMAIL 101| Rahul| rahul@gmail.com
102| Priya| priya@gmail.com
103| Amit| amit@gmail.com
- What Happens If You Insert a Duplicate PRIMARY KEY?
Suppose a developer tries to insert another employee with employee_id 101.
INSERT INTO employees VALUES (101, 'Sneha', 'sneha@gmail.com');
Will this record be inserted?
No.
The INSERT statement fails because employee_id 101 already exists.
Why?
Because PRIMARY KEY does not allow duplicate values.
It also does not allow NULL values.
A primary key is used to identify each record uniquely in a table.
- What Happens If You Insert a Duplicate UNIQUE Value?
Now let's try something different.
The developer uses a new employee ID but enters an email address that already belongs to Rahul.
INSERT INTO employees VALUES (104, 'Sneha', 'rahul@gmail.com');
What happens now?
The INSERT statement fails because "rahul@gmail.com" already exists in the employees table.
The employee ID is different, but the email address is repeated.
The UNIQUE constraint prevents this duplicate email from being inserted.
This is why we use UNIQUE constraints for columns such as email addresses.
- PRIMARY KEY vs UNIQUE: Main Differences
Both constraints prevent duplicate values, but there are some important differences.
PRIMARY KEY| UNIQUE Does not allow duplicate values| Does not allow duplicate non-NULL values Does not allow NULL| Allows NULL Only one per table| Multiple per table Identifies each record| Ensures unique values
A UNIQUE constraint does not automatically make a column NOT NULL.
For example, if every employee must have an email address, you can use both UNIQUE and NOT NULL.
CREATE TABLE employees ( employee_id NUMBER PRIMARY KEY, employee_name VARCHAR2(50), email VARCHAR2(100) UNIQUE NOT NULL );
Now the email column cannot contain NULL values or duplicate email addresses.
- Can We Use PRIMARY KEY and UNIQUE Together?
Yes. A table can have both constraints.
For example:
- "employee_id" → PRIMARY KEY
- "email" → UNIQUE
The employee ID identifies each employee, while the email constraint prevents duplicate email addresses.
A table can have only one PRIMARY KEY constraint, but it can have multiple UNIQUE constraints.
This is a common approach when designing database tables.
- SQL Interview Question: Test Your Understanding
Let's check your understanding with a practical question.
Assume the employees table already contains these records:
ID| Name| Email 101| Rahul| rahul@gmail.com 102| Priya| priya@gmail.com 103| Amit| amit@gmail.com
Now execute this SQL statement:
INSERT INTO employees VALUES (104, 'Sneha', 'rahul@gmail.com');
What will happen?
Option A: The record will be inserted because employee_id 104 is new.
Option B: The INSERT will fail because the email address already exists.
Option C: Oracle will automatically change the email address.
Option D: Oracle will reject the table because PRIMARY KEY and UNIQUE cannot be used together.
Correct Answer: Option B
The employee ID 104 is new, but the email address already belongs to Rahul.
The UNIQUE constraint prevents the INSERT statement from completing successfully.
Final Thoughts
Remember these two simple rules:
- Use PRIMARY KEY when you need to identify each row in a table.
- Use UNIQUE when you want to prevent duplicate values in a column or combination of columns.
For example, in an employee database, employee_id is a good choice for PRIMARY KEY, while email is a good choice for UNIQUE.
Understanding these two constraints will help you create better database tables and avoid duplicate data.
Have you used PRIMARY KEY and UNIQUE together in your SQL projects? Share your experience.
Comments
Join the discussionNo comments yet. Be the first to comment.