- Primary key: uniquely identifies mỗi row, NOT NULL, UNIQUE, immutable — thường là surrogate key (serial/UUID).
- Composite PK:
PRIMARY KEY (order_id, product_id)trong junction tables. - Foreign key: đảm bảo referential integrity — không thể insert
orders.user_id = 999nếu users.id = 999 không tồn tại; không thể delete user nếu còn orders tham chiếu (trừ khi có CASCADE). - FK constraint options:
RESTRICT(default, block delete/update nếu có children),CASCADE(propagate delete/update xuống children),SET NULL(set FK = NULL khi parent deleted — cần FK column nullable),SET DEFAULT(set FK = default value),NO ACTION(like RESTRICT nhưng deferred). - Indexing: PK tự động có index; FK column nên index để JOIN và DELETE parent nhanh — thiếu index trên FK gây full scan child table mỗi khi delete/update parent.
Pitfall: FK constraints enforce data integrity nhưng có overhead mỗi INSERT/UPDATE/DELETE — một số teams disable FK trong high-throughput systems và enforce integrity ở application layer (trade-off consistency vs performance).