What is a primary key?
Answer
A primary key is a column (or combination of columns) that uniquely identifies each row in a table. Every table should have a primary key. Properties of a primary key: (1) Unique — no two rows can have the same primary key value; (2) Not NULL — a primary key column cannot contain NULL; (3) Immutable (best practice) — primary key values should not change once set. Common types: (1) Auto-increment integer: id INT AUTO_INCREMENT PRIMARY KEY — MySQL assigns the next integer automatically on each insert; (2) UUID/GUID: globally unique identifier — good for distributed systems but larger storage and slower index performance than integers; (3) Natural key: a real-world attribute that is naturally unique (email, SSN) — but natural keys can change or have format issues, so surrogate (synthetic) keys are usually preferred. Composite primary key: when no single column is unique, combine two or more columns: PRIMARY KEY (order_id, product_id) — common in junction tables.