Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> SQL

Candidate Key ใน RDBMS


แต่ละตารางมี Primary Key เพียงตัวเดียว แต่ relation อาจมี Candidate Key หลายตัว หนึ่งใน Candidate Key จะถูกเลือกเป็น Primary Key ทุก Candidate Key มีคุณสมบัติเหมาะสมเป็น Primary Key ดังนั้น Candidate Key คือ "ผู้สมัคร" สำหรับ Primary Key

Candidate Key อาจเป็นคอลัมน์เดียว หรือการรวมหลายคอลัมน์ Minimal Super Key เรียกว่า Candidate Key

ตัวอย่าง

EmployeeID และ EmployeeEmail ทั้งคู่เป็น Primary Key ได้ ดังนั้นทั้งคู่เป็น Candidate Keys เลือกอย่างใดอย่างหนึ่งเป็น Primary Key

ตัวอย่างเพิ่มเติม

Student_ID Student_Enroll Student_Name Student_Email
S02 4545 Dave ddd@gmail.com
S34 4541 Jack jjj@gmail.com
S22 4555 Mark mmm@gmail.com

Student_ID, Student_Enroll และ Student_Email เป็น Candidate Keys เพราะระบุนักเรียนได้อย่างเฉพาะเจาะจง

ลำดับชั้นของ Keys

  • Super Key: ชุดแอตทริบิวต์ใดๆ ที่ unique
  • Candidate Key: Super Key ที่ minimal (ไม่มี subset ที่เป็น super key)
  • Primary Key: Candidate Key ที่เลือกใช้เป็นหลัก
  • Alternate/Secondary Key: Candidate Keys ที่เหลือ
  • Foreign Key: อ้างอิง Primary Key ของตารางอื่น