SQL PRIMARY KEY Constraint

Last Updated : 24 Aug, 2026

The PRIMARY KEY constraint in SQL uniquely identifies each record in a table and ensures strong data integrity. It prevents duplicate and NULL values, making it essential for reliable relational database design.

  • Ensures all values are unique.
  • Does not allow NULL values.
  • Only one primary key per table (can be composite).
  • Automatically creates a unique index for faster searches.

Query:

CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
department VARCHAR(50)
);

-- Insert data into employees table
INSERT INTO employees (emp_id, emp_name, department)
VALUES
(101, 'Bob', 'Sales'),
(102, 'Lucas', 'HR');

Output:

Screenshot-2026-08-24-153741
  • EmpID is the primary key, so it must be unique and cannot be NULL.
  • If you try inserting duplicate or NULL EmpID values, the database will throw an error.

Query:

-- Duplicate primary key value (EmpID = 101 already exists)
INSERT INTO Employees VALUES (101, 'Alice', 'IT');

-- NULL not allowed in primary key column
INSERT INTO Employees VALUES (NULL, 'Emma', 'Finance');

Error: