Hook: Interviewers love this one because a primary key is the table's identity card: if the identity is weak, every join, update, and lookup becomes risky.
Question: What is a primary key in SQL?
Answer: A primary key is the column or set of columns that uniquely identifies each row in a table. Its values must be unique and cannot be NULL, so every row has one clear identity. A table can have only one primary key constraint, but that key can be made from multiple columns.
Interview-Ready Answer: In SQL, a primary key is the main identifier for a row. I use it when I need each row to be uniquely and reliably found, and I know it cannot be null or duplicated. A useful detail is that most databases back it with a unique index, so lookups and joins on the key are fast.
Detailed Explanation: Think of a primary key as the row's official name tag. It is not just a column with unique values; it is a rule enforced by the database that says, 'this value, or this combination of values, must point to exactly one row and never be missing.' That is why the database uses it for integrity, joins, and fast access.
NULL values in the key columns.INSERT or key-changing UPDATE, the database checks the index first. If the key already exists, the statement fails.A lot of candidates mix these up. Both enforce uniqueness, but they are not the same job. A primary key is the main identity of the row. A unique constraint is an extra rule that says 'this other value must also stay unique.'
| Feature | PRIMARY KEY | UNIQUE |
|---|---|---|
| Main purpose | Row identity | Extra uniqueness rule |
| NULL allowed? | No | Often yes, but DBMS rules vary |
| Count per table | One | Many |
| Composite allowed? | Yes | Yes |
| Common FK target | Yes | Sometimes, if supported and not null |
Because the primary key is usually backed by an index, lookup is typically O(log n) rather than a full table scan. In practice, a B-tree index on a table with millions of rows often has only a few levels, so the database may find the row in about 3 to 4 page reads. Inserts and updates are a little more expensive than a heap with no index, because the index must also be maintained.
There are a few real gotchas:
VARCHAR email key takes more space than an integer key, so every index and child table becomes heavier.(order_id, line_no).UNIQUE constraints.Memory check: 'Primary key = the row's passport number: one per row, never blank, never shared.'
Real-World Story: Imagine a checkout service for an online store. The orders table should use order_id as the primary key, because one customer can place many orders. A team once used customer_id instead, which meant the second order from the same customer failed with a duplicate-key error or got overwritten by an upsert. Users saw missing orders, support saw duplicate-key logs, and warehouse staff almost shipped the wrong items because the system could no longer tell orders apart. The fix was simple but important: keep order_id as the primary key, and keep customer_id as a normal foreign key that points back to customers.
-- Primary key demo: one table with a single-column primary key
-- and one table with a composite primary key.
CREATE TABLE pk_demo_customers (
customer_id INTEGER PRIMARY KEY,
full_name VARCHAR(100) NOT NULL
);
CREATE TABLE pk_demo_order_lines (
order_id INTEGER NOT NULL,
line_no INTEGER NOT NULL,
sku VARCHAR(30) NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, line_no)
);
INSERT INTO pk_demo_customers (customer_id, full_name)
VALUES
(1, 'Ava Chen'),
(2, 'Noah Patel');
INSERT INTO pk_demo_order_lines (order_id, line_no, sku, quantity)
VALUES
(1001, 1, 'USB-C-CABLE', 2),
(1001, 2, 'CHARGER-65W', 1),
(1002, 1, 'MOUSE-WIRELESS', 1);
-- This query shows the rows that were accepted because each key is unique.
SELECT *
FROM pk_demo_customers
ORDER BY customer_id;
SELECT *
FROM pk_demo_order_lines
ORDER BY order_id, line_no;
-- Edge case: these statements would fail because a primary key cannot be NULL or duplicated.
-- Uncomment one at a time in a test database to see the constraint in action.
-- INSERT INTO pk_demo_customers (customer_id, full_name) VALUES (1, 'Duplicate Ava');
-- INSERT INTO pk_demo_customers (customer_id, full_name) VALUES (NULL, 'Missing Key');
-- INSERT INTO pk_demo_order_lines (order_id, line_no, sku, quantity) VALUES (1001, 1, 'DUPLICATE-LINE', 1);
-- Why this matters: if the key is not protected, two different rows can pretend to be the same row,
-- and that breaks updates, joins, and reporting.Follow-up & Tricky Questions:
NULL? No. If a value is missing, the database cannot use it to identify a row, so primary key columns are always not null.Common Mistakes:
Memory Hook: Primary key = a table's passport number: one per row, never blank, never shared.
Cheat Sheet:
NULL.Practice Tasks:
customers table with an integer primary key and insert three rows.(invoice_id, line_no), and try inserting a duplicate row.NULL in your DBMS.