- Uniqueness: ✅
- No two rows can have the same value (or same combination, for composite keys).
- NOT NULL: ✅
- No NULLs allowed in any column that is part of the Primary Key.
- Every row must have a real, non-missing value for the Primary Key.
- Automatic Index: ✅
- A unique index is automatically created behind the scenes.
- This makes finding, updating, and deleting rows much faster.
- Data Integrity: ✅
- Ensures your data has a reliable way to uniquely identify each row.
- Foreign Key Relationships: ✅
- Other tables can create foreign keys pointing to this Primary Key.
- This is how you build relationships between tables (like Users → Orders).
📝 In Short
Feature | Primary Key Ensures |
---|---|
Uniqueness | No duplicate key values. |
Not Null | No NULL allowed in the key. |
Fast Lookup | Creates a unique index for fast access. |
Data Integrity | Each row is reliably identified. |
Relationships | Other tables can refer to it (foreign keys). |
Example
CREATE TABLE Users (
user_id INT PRIMARY KEY,
name VARCHAR(100)
);
✅ What happens now:
user_id
must be unique.user_id
cannot be NULL.- A unique index on
user_id
is created automatically. - You can easily do:
user_id
must be unique.
user_id
cannot be NULL.
A unique index on user_id
is created automatically.
You can easily do:
SELECT * FROM Users WHERE user_id = 123;
and it will be fast.
So — to your question:
When I define a primary key, it means I cannot insert a record with the same primary key, and that is it?
✅ YES — plus:
- No NULLs allowed in that column.
- Fast searching thanks to the index.
- Safe relationships via foreign keys.
It’s not only about uniqueness — it’s about uniqueness + not null + fast access + relational integrity.
Visual Analogy
Primary Key is like a passport number:
- Unique — no two people can have the same passport number.
- Mandatory — you must have one (can’t be NULL).
- Index — the government database can quickly find you using it.
- Link — other systems (airlines, visa offices) can reference it.