Relational Database Design
Normalisation là gì?
Ba dạng chuẩn của Codd: 1NF (the key), 2NF (the whole key), 3NF (nothing but the key). Kèm denormalisation.
Normalisation là gì?
Chuỗi rules để đảm bảo chất lượng relational data model. Phải đạt từng normal form theo thứ tự: 1NF → 2NF → 3NF.
"Every non-key attribute must provide a fact about the key, the whole key, and nothing but the key" — William Kent
Primary Key & Foreign Key
Primary Key (PK): xác định duy nhất một row. Có 2 loại: Natural Key (dữ liệu thực tế) và Surrogate Key (artificial, vd: auto-increment ID).
Foreign Key (FK): tham chiếu đến PK của bảng liên quan. Thực hiện relationship giữa 2 bảng.
1NF – "The Key"
Mọi non-key field phải cung cấp fact về key. Không có multivalue fields hoặc repeating groups. Tất cả rows phải có cùng số fields.
Ví dụ: Nếu một Tour có nhiều Keywords → tạo bảng TourKeywords riêng.
2NF – "The Whole Key"
Áp dụng khi PK là composite key. Mọi non-key field phải phụ thuộc vào toàn bộ PK (không chỉ một phần).
Ví dụ: LineItems(InvoiceNo, ProductName, Quantity, Manufacturer) — Manufacturer chỉ phụ thuộc ProductName → tách ra bảng Products.
3NF – "Nothing But the Key"
Mọi non-key field chỉ phụ thuộc vào key field, không phụ thuộc vào non-key field khác. Loại bỏ transitive dependencies.
Ví dụ: Nếu DifficultyDescription phụ thuộc vào DifficultyRating (non-key) → tách ra bảng TourDifficulty.
Denormalisation
Đảo ngược normalisation khi cần: (1) Cải thiện performance, (2) Lưu historical data (vd: Invoice.Address copy từ Customer.Address để lưu địa chỉ tại thời điểm mua).