When you define a Primary Key on a table:
- 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_idmust be unique.user_idcannot be NULL.- A unique index on
user_idis created automatically. - You can easily do: