Imagine you are working on an employee management system.
The company stores employee details in a database. Every employee has an employee ID, name, department and salary.
Now, a developer tries to insert a new employee using an ID that already exists in the table.
Will SQL accept the new record, replace the existing employee or return an error?
Let's understand this with a practical SQL example.
- Create an Employees Table
First, create an "employees" table with a PRIMARY KEY on "employee_id".
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10,2)
);
Here, "employee_id" is the PRIMARY KEY.
It means every employee must have a unique ID.
- Insert Existing Employee Records
Let's insert three employee records.
INSERT INTO employees
(employee_id, employee_name, department, salary)
VALUES
(101, 'Rahul', 'IT', 65000),
(102, 'Priya', 'HR', 55000),
(103, 'Amit', 'Finance', 60000);
Employees Table:
employee_id| employee_name| department| salary
101| Rahul| IT| 65000
102| Priya| HR| 55000
103| Amit| Finance| 60000
So far, everything is working correctly.
- Try to Insert a Duplicate Employee ID
Now, a developer tries to insert another employee with "employee_id = 101".
INSERT INTO employees
(employee_id, employee_name, department, salary)
VALUES (101, 'Sneha', 'IT', 70000);
What will happen?
The INSERT statement will fail because employee ID 101 already exists as a PRIMARY KEY.
The database returns a primary key or unique constraint violation error. The exact error message depends on the database.
- What Will the Final Table Look Like?
The original records remain unchanged.
employee_id| employee_name| department| salary
101| Rahul| IT| 65000
102| Priya| HR| 55000
103| Amit| Finance| 60000
Sneha's record will not be inserted.
- Important Points About PRIMARY KEY
A PRIMARY KEY has two important rules:
- It does not allow duplicate values.
- It does not allow NULL values.
A PRIMARY KEY helps uniquely identify every record in a table.
For example, it is commonly used for:
- Employee IDs
- Customer IDs
- Order IDs
- Product IDs
These rules help maintain correct and reliable data in a database.
Your Turn: Test Your SQL Knowledge
Can a table have two PRIMARY KEY constraints?
A. Yes, every column can have a separate PRIMARY KEY.
B. No, a table can have only one PRIMARY KEY constraint, but it can contain multiple columns (a composite PRIMARY KEY).
C. Yes, but only when both columns have different data types.
D. No, a PRIMARY KEY can contain only one column.
Share your answer in the comments!
Learn More About SQL Constraints
If you want to learn more about SQL CREATE TABLE, PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL and other database constraints, explore these practical SQL tutorials:
"Sankalan Data Tech – SQL DDL Commands and Database Constraints" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)

Top comments (0)