แน่นอนว่าข้อมูลที่เราขว้างลงไปในฐานข้อมูลนั้นแต่ละเรคคอร์ดย่อมสามารถเข้าถึงได้ด้วยคีย์ที่เรียกว่า Primary Key (PK) ซึ่งที่เราเรียนมามันก็เป็นหนึ่งในคีย์ของ Relational Keys (คีย์ที่ใช้ identify แต่ละเรคคอร์ด) และไอ้ Relational Keys ทันก็ทำให้เราสับสนเป็นไก่ตาแตกมาถึงทุกวันนี้ อาจารย์อธิบายเท่าไหร่ก็ไม่เข้าใจ ไปอ่านหนังสือก็ยิ่งไม่เข้าใจใหญ่ วันนี้เราจะมาดูความแตกต่างของคีย์แต่ละชนิดกันครับ
Relational Keys
การที่เราจะจำแนก (identify) แต่ละเรคคอร์ดนั้น เราจะเลือกแอตทริบิวต์ที่ไม่มีทางซ้ำกันแน่นอนในนารางนั้น ๆ มาเป็นตัวที่ใช้จำแนก เรียกว่า Relational Keys มีอยู่ 4 ชนิดด้วยกันคือ
- Superkey
- An attribute, or set of attributes, that uniquely identifies a tuple within a relaion.
- Candidate key
- A superkey such that no proper subset is a superkey within the relation.
- Primary key
- The candidate key that is seleted to identify tuples uniquely within relation.
- Foreign key
- An attribute, or set of attriute, within one relation that matches the candidate key of some relation.
ในส่วนที่เราจะสับสนกับคือ Superkey กับ Candidate key ซึ่งจากที่รวบรวมข้อมูลได้มาความแตกต่างของทั้งสองอย่างมีดังนี้
