SQL PRIMARY KEY Get Certified
Learn how the PRIMARY KEY constraint uniquely identifies every row in a table and forms the foundation for relationships between tables.
What is a PRIMARY KEY?
A primary key is a column (or a group of columns) whose values uniquely identify each row. It has two essential properties:
- Every value must be unique.
- NULL values are never allowed.
A table may have only one primary-key definition.
Basic Syntax
CREATE TABLE students (
student_id INTEGER PRIMARY KEY,
student_name TEXT,
course TEXT
);
INTEGER PRIMARY KEY (Automatic Values)
In SQLite an INTEGER PRIMARY KEY column automatically receives a unique integer when you omit the value on INSERT.
What this code does: Creates a books table and lets SQLite assign the book_id values.
DROP TABLE IF EXISTS books;
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT NOT NULL
);
INSERT INTO books (title, author) VALUES
('The Silent Planet', 'Riya Sen'),
('Beyond the Horizon', 'Arjun Rao'),
('Digital Dreams', 'Meera Kapoor');
SELECT *
FROM books
ORDER BY book_id;
Manually Assigned Primary-Key Values
You may also supply the primary-key values yourself, provided each one is unique.
What this code does: Inserts three products with explicit product_id values.
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
price REAL
);
INSERT INTO products (product_id, product_name, price) VALUES
(501, 'Wireless Speaker', 2499.00),
(502, 'Desk Lamp', 1299.00),
(503, 'Travel Charger', 899.00);
SELECT *
FROM products
ORDER BY product_id;
PRIMARY KEY Prevents Duplicates
Attempting to insert a second row with an existing primary-key value raises a constraint error. The example below shows only the valid rows so that the script remains fully executable.
What this code does: Creates an inventory table and inserts three unique item_id values.
DROP TABLE IF EXISTS inventory;
CREATE TABLE inventory (
item_id INTEGER PRIMARY KEY,
item_name TEXT NOT NULL,
quantity INTEGER
);
INSERT INTO inventory VALUES
(1, 'Steel Bolts', 120),
(2, 'Copper Wire', 75),
(3, 'Aluminum Sheets', 40);
SELECT *
FROM inventory
ORDER BY item_id;
PRIMARY KEY Together with Other Constraints
A primary key can coexist with NOT NULL, UNIQUE, CHECK and DEFAULT on the same table.
What this code does: Creates an employees table that combines several constraints.
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT NOT NULL,
email TEXT UNIQUE,
age INTEGER CHECK (age >= 18),
status TEXT DEFAULT 'Active'
);
INSERT INTO employees (employee_name, email, age) VALUES
('Aarav', 'aarav@example.com', 28),
('Nisha', 'nisha@example.com', 31),
('Kabir', 'kabir@example.com', 25);
SELECT *
FROM employees
ORDER BY employee_id;
PRIMARY KEY Referenced by a FOREIGN KEY
Foreign keys normally point at a primary key, creating a relationship between two tables.
What this code does: Creates customers (parent) and orders (child) linked by customer_id.
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
product_name TEXT NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
INSERT INTO customers (customer_name) VALUES
('Rohan'),
('Priya'),
('Sameer');
INSERT INTO orders (customer_id, product_name) VALUES
(1, 'Laptop Stand'),
(2, 'Wireless Mouse'),
(1, 'USB Hub');
SELECT
orders.order_id,
customers.customer_name,
orders.product_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
ORDER BY orders.order_id;
Composite PRIMARY KEY
When uniqueness depends on more than one column you define a composite primary key. The combination of values must be unique, even if individual columns contain duplicates.
What this code does: Uses (student_id, course_id) as a composite primary key for enrollments.
DROP TABLE IF EXISTS enrollments;
CREATE TABLE enrollments (
student_id INTEGER,
course_id INTEGER,
enrollment_date TEXT,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO enrollments VALUES
(1, 101, '2026-08-10'),
(1, 102, '2026-08-11'),
(2, 101, '2026-08-12'),
(3, 103, '2026-08-13');
SELECT *
FROM enrollments
ORDER BY student_id, course_id;
Inspecting the PRIMARY KEY Definition
PRAGMA table_info() returns a pk column that is non-zero for every part of the primary key.
What this code does: Shows which column is the primary key of the departments table.
DROP TABLE IF EXISTS departments;
CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
department_name TEXT NOT NULL,
location TEXT
);
SELECT
cid,
name,
type,
pk
FROM pragma_table_info('departments')
ORDER BY cid;
PRIMARY KEY with AUTOINCREMENT
Adding the keyword AUTOINCREMENT guarantees that SQLite never re-uses a previously deleted row-id value.
What this code does: Creates a tickets table whose ticket_id values grow permanently.
DROP TABLE IF EXISTS tickets;
CREATE TABLE tickets (
ticket_id INTEGER PRIMARY KEY AUTOINCREMENT,
subject TEXT NOT NULL
);
INSERT INTO tickets (subject) VALUES
('Login issue'),
('Payment question'),
('Profile update');
SELECT *
FROM tickets
ORDER BY ticket_id;
PRIMARY KEY vs UNIQUE
Common PRIMARY KEY Uses
Important Points
- A primary key uniquely identifies every row.
- A table can have only one primary-key definition.
- Primary-key values cannot be NULL and cannot be duplicated.
- A primary key may consist of one column or several columns (composite).
- Foreign keys normally reference a primary key.
- SQLite’s
INTEGER PRIMARY KEYsupplies automatic row identifiers.
Best Practices
- Prefer a simple integer surrogate key when business data may change.
- Keep primary-key values stable after a row is created.
- Use a composite primary key only when uniqueness truly depends on several columns.
- Always define foreign keys that reference existing primary keys.
- Avoid using frequently-changing business attributes (email, phone, etc.) as the primary key.
The PRIMARY KEY constraint is the cornerstone of relational design. It gives every row a reliable identity and provides the target for foreign-key relationships. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.