Constraints: PRIMARY KEY, NOT NULL, DEFAULT
Protect your data with the three essential constraints — unique IDs, required values and automatic defaults.
What stops someone from adding two players with the same id, or a player with no name? Constraints. They are rules you attach to columns so the database itself refuses bad data — no matter which app or person tries to insert it.
Definition
A constraint is a rule defined on a column or table that the database automatically enforces on every
INSERTandUPDATE. If a change would break the rule, the database rejects the whole statement with an error.
- Automatically enforces — you don't have to remember to check; the database checks for you, every time.
- Rejects the statement — the bad row is never saved. The table stays clean.
This topic covers the three constraints from the course slides. The next topic covers UNIQUE, CHECK and FOREIGN KEY.
| Constraint | Rule | Example |
|---|---|---|
PRIMARY KEY | Unique ID for every row — no duplicates, no NULL | id INT PRIMARY KEY |
NOT NULL | A value is required — the column can never be empty | name VARCHAR(50) NOT NULL |
DEFAULT | An automatic value used when none is given | runs INT DEFAULT 0 |
PRIMARY KEY
A PRIMARY KEY is a column (or set of columns) whose value uniquely identifies each row in a table. It can never be duplicated and can never be NULL. A table can have only one primary key.
Think of it as a player's unique jersey ID in the database. Two players may share the name "Rohit", but never the same id.
| Insert | Result |
|---|---|
id = 7 (new value) | ✅ OK |
id = 1 again (already used by Virat Kohli) | ❌ ERROR — Violation of PRIMARY KEY constraint 'pk_players'. Cannot insert duplicate key. |
id = NULL | ❌ ERROR — a primary key column cannot be NULL |
Two ways to write it:
sql
-- Column level (quick)
id INT PRIMARY KEY
-- Table level, with your own name (recommended)
CONSTRAINT pk_players PRIMARY KEY (id)A composite primary key uses more than one column, e.g. in a scorecard table one player can bat in many matches, but only once per match: PRIMARY KEY (match_id, player_id).
NOT NULL
NOT NULL means the column must always contain a value. Inserting a row without it, or setting it to
NULL, is rejected.
| Insert | Result |
|---|---|
name = 'Dhoni' | ✅ OK |
name = NULL or name left out | ❌ ERROR — Cannot insert the value NULL into column 'name' |
NULL means "unknown / missing" — it is not zero and not an empty string. Columns without NOT NULL allow NULL by default.
DEFAULT
DEFAULT gives a column an automatic value that SQL uses when an
INSERTdoes not provide a value for that column.
| Insert | Result |
|---|---|
runs left out of the INSERT | ✅ AUTO — runs = 0 is used |
runs = 120 given | ✅ 120 is used (the default is ignored) |
A new player starts the season at 0 runs — perfect use of DEFAULT 0.
Note: The default is used only when the column is left out of the INSERT. If you explicitly insert
NULL, the column gets NULL (unless it is alsoNOT NULL).
Example — all three constraints together
sql
CREATE TABLE players_new (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
team VARCHAR(10),
runs INT DEFAULT 0,
CONSTRAINT pk_players_new PRIMARY KEY (id)
);
INSERT INTO players_new (id, name, team) -- runs left out
VALUES (7, 'Shubman Gill', 'GT');
INSERT INTO players_new (id, name, team, runs) -- runs given
VALUES (8, 'Rinku Singh', 'KKR', 120);
SELECT * FROM players_new;| id | name | team | runs |
|---|---|---|---|
| 7 | Shubman Gill | GT | 0 |
| 8 | Rinku Singh | KKR | 120 |
How it works: Gill's row left out runs, so the DEFAULT 0 filled it in. Rinku's row gave 120, so the default was ignored.
Bonus — IDENTITY (auto-numbering in SQL Server)
Typing ids by hand is tedious. SQL Server can generate them:
sql
CREATE TABLE fans (
fan_id INT IDENTITY(1,1) PRIMARY KEY, -- starts at 1, adds 1 each row
name VARCHAR(50) NOT NULL
);
INSERT INTO fans (name) VALUES ('Ravi'), ('Priya'); -- fan_id becomes 1, 2IDENTITY(seed, increment) — seed is the first number, increment is the step. MySQL calls this AUTO_INCREMENT; PostgreSQL uses GENERATED ALWAYS AS IDENTITY (or SERIAL).
Common mistakes
- ❌ Two
PRIMARY KEYclauses on different columns. ✅ Only one primary key per table — use a composite keyPRIMARY KEY (a, b)if you need two columns. - ❌ Thinking
DEFAULTstops NULLs. ✅DEFAULTonly fills missing values; addNOT NULLto block explicit NULLs. - ❌ Confusing NULL with 0 or
''. ✅ NULL means unknown; 0 is a real number. - ❌ Leaving constraints unnamed. ✅ Name them (
pk_players) — the error messages become readable and you can drop them later by name.
Interview tip
Q: Can a primary key contain NULL values? Can a table have two primary keys?
A: No and no. A primary key is automatically NOT NULL and UNIQUE, and a table has at most one primary key — though that one key may be made of several columns (a composite key).
Key points to remember
- A constraint is a rule the database enforces automatically on every INSERT and UPDATE — bad data is rejected with an error.
- PRIMARY KEY uniquely identifies each row: no duplicates and no NULLs; only one per table (it may span several columns).
- NOT NULL makes a value required — leaving it out or inserting NULL fails.
- DEFAULT supplies an automatic value when the INSERT leaves the column out (e.g. runs INT DEFAULT 0).
- Name your constraints with CONSTRAINT name ... (e.g. pk_players) for clear errors and easy maintenance.
- NULL means unknown — it is not 0 and not an empty string.
- SQL Server generates automatic ids with IDENTITY(seed, increment); MySQL uses AUTO_INCREMENT.
Try it yourself
DEFAULT fills in a missing value
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
See the PRIMARY KEY and NOT NULL rules fail
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SQL Server: IDENTITY auto-numbering
CREATE TABLE fans (
fan_id INT IDENTITY(1,1) NOT NULL,
name VARCHAR(50) NOT NULL,
CONSTRAINT pk_fans PRIMARY KEY (fan_id)
);
INSERT INTO fans (name)
VALUES ('Ravi'), ('Priya'), ('Arjun');
SELECT * FROM fans;
-- fan_id | name
-- 1 | Ravi
-- 2 | Priya
-- 3 | ArjunSQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: add a default country
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
