🐘 PostgreSQL
Beginner
What is a PRIMARY KEY in PostgreSQL?
Answer
A PRIMARY KEY is a column (or combination of columns) that uniquely identifies each row in a table. It automatically enforces NOT NULL and UNIQUE constraints. Every table should have a primary key for data integrity and efficient lookups. PostgreSQL automatically creates a unique B-tree index on the primary key column(s). Common patterns: id SERIAL PRIMARY KEY (auto-incrementing integer), id UUID PRIMARY KEY DEFAULT gen_random_uuid() (universally unique ID). Composite primary keys: PRIMARY KEY (user_id, role_id) at the table level. Choose BIGSERIAL (64-bit) over SERIAL (32-bit) for tables expected to have more than 2 billion rows.