SQL Challenge : What Happens When You Insert a Duplicate PRIMARY KEY?
Have you ever tried to insert a record into a SQL table using an ID that already exists?
If the column is defined as a PRIMARY KEY, SQL will not allow the duplicate value.
Let's understand why this happens with a simple example from an employee management system.
- Create an Employees Table
Imagine a company wants to store employee details such as employee ID, name, department and salary.
The table can be created using the following SQL statement:
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. Duplicate and NULL values are not allowed in this column.
- Insert Employee Records
Let's insert three employee records into the table.
INSERT INTO employees (employee_id, employee_name, department, salary) VALUES (101, 'Rahul', 'IT', 65000), (102, 'Priya', 'HR', 55000), (103, 'Amit', 'Finance', 60000);
Now, the employees table looks like this:
employee_id| employee_name| department| salary
101| Rahul| IT| 65000
102| Priya| HR| 55000
103| Amit| Finance| 60000
All three records are inserted successfully because each employee has a different ID.
- Try to Insert a Duplicate Employee ID
Now, imagine another employee named Sneha joins the company.
A developer tries to insert her details using employee ID 101.
INSERT INTO employees (employee_id, employee_name, department, salary) VALUES (101, 'Sneha', 'IT', 70000);
Will this INSERT statement work?
No. The database will reject the INSERT statement because employee ID 101 already exists.
The PRIMARY KEY constraint prevents duplicate values in the same column.
The exact error message depends on the database system, but it will be a primary key or unique constraint violation.
- What Will the Final Table Look Like?
The original employee records will 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.
SQL will not automatically replace Rahul's record or generate another employee ID.
- Important Points About PRIMARY KEY
A PRIMARY KEY is used to identify each record uniquely in a database table.
Remember these two important rules:
- A PRIMARY KEY does not allow duplicate values.
- A PRIMARY KEY does not allow NULL values.
You will commonly find PRIMARY KEY constraints in employee, customer, order and product tables.
They help prevent duplicate records and maintain correct data in a database.
- Test Your SQL Knowledge
Here is one more question to check your understanding.
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.
C. Yes, but only when both columns have different data types.
D. No, a PRIMARY KEY can contain only one column.
What is your answer? Share it in the comments and explain why.
Learn More SQL Concepts
If you are learning SQL or preparing for SQL developer and data analyst interviews, understanding database constraints is important.
For more practical examples of SQL CREATE TABLE, PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL and other DDL commands, visit:
"SQL DDL Commands and Database Constraints – Sankalan Data Tech" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)
Keep practising SQL with real-world examples. The more you practise, the easier it becomes to understand how databases work.
Comments
Join the discussionNo comments yet. Be the first to comment.