Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> SQL

Super Key (Khóa siêu) trong RDBMS


Super Key (Khóa siêu) là một thuộc tính (attribute) hoặc tập hợp các thuộc tính có thể **duy nhất xác định** một tuple (hàng) trong quan hệ (bảng). Super Key là khái niệm bao quát hơn Candidate Key.

Định nghĩa chính thức

  • Super Key là tập hợp thuộc tính K sao cho không có 2 tuple nào có cùng giá trị trên K.
  • Super Key là **siêu tập (superset)** của Candidate Key. Mọi Candidate Key đều là Super Key, nhưng ngược lại không đúng.
  • Super Key có thể chứa thuộc tính "thừa" (không cần thiết để duy nhất).

Ví dụ minh họa

Bảng Student:

Student_ID Student_Enroll Student_Name Student_Email
S024545Daveddd@gmail.com
S344541Jackjjj@gmail.com
S224555Markmmm@gmail.com

Các Super Key của bảng Student

Giả sử: Student_ID, Student_Enroll, Student_Email đều duy nhất. Student_Name KHÔNG duy nhất (có thể trùng tên).

{Student_ID}
{Student_Enroll}
{Student_Email}
{Student_ID, Student_Enroll}
{Student_ID, Student_Name}
{Student_ID, Student_Email}
{Student_Enroll, Student_Name}
{Student_ID, Student_Enroll, Student_Name}
{Student_ID, Student_Enroll, Student_Email}
{Student_ID, Student_Enroll, Student_Name, Student_Email}

Candidate Key (Khóa ứng viên) - Tập con tối thiểu của Super Key

Loại bỏ các thuộc tính thừa từ Super Key để được tập hợp tối thiểu vẫn đảm bảo duy nhất:

{Student_ID}
{Student_Enroll}
{Student_Email}

→ 3 Candidate Key. Chọn 1 làm Primary Key, 2 còn lại thành Alternate/Secondary Key.

Đặc điểm quan trọng

  • Mỗi bảng có **ít nhất 1 Super Key** (tất cả các cột ghép lại luôn là Super Key).
  • Super Key có thể là **một thuộc tính đơn lẻ** (nếu nó duy nhất) hoặc **tổ hợp nhiều thuộc tính**.
  • Primary Key **luôn là** một Super Key (và là Candidate Key).
  • Super Key **không nhất thiết** là Candidate Key (có thể dư thừa).

Ví dụ Super Key KHÔNG phải Candidate Key

{Student_ID, Student_Name} là Super Key (vì Student_ID đã duy nhất), nhưng **KHÔNG** phải Candidate Key vì bỏ Student_Name đi vẫn duy nhất (Student_ID là tập con).

Tóm tắt mối quan hệ

Super Key ⊇ Candidate Key ⊇ Primary Key
     ↑                ↑
  Có thể dư thừa   Tối thiểu (minimal)
  (có thừa)        (không thừa)

Ứng dụng: Khi thiết kế DB, xác định tất cả Super Key -> lọc ra Candidate Key (tối thiểu) -> chọn Primary Key -> các Candidate Key còn lại thành Alternate Key.