Explain different types of keys in a relational database along with example.
Solution
Key Types in the Relational Model
| Key Type | Definition | Example |
|---|---|---|
| Super Key | Any set of attributes that uniquely identifies every tuple in a relation | {sid}, {sid, name}, {email} |
| Candidate Key | A minimal super key — no proper subset is also a super key | {sid}, {email} |
| Primary Key | The one candidate key chosen by the designer (underlined in schema) | sid |
| Foreign Key | An attribute in one relation that references the PK of another relation | Enrollment.sid → Student.sid |
| Partial Key | Attribute(s) that partially identify a weak entity when combined with the owner's PK | item_no in Order_Item (combined with order_id) |
Candidate Key vs Primary Key
- A relation can have multiple candidate keys (e.g., both
sidandemailcould uniquely identify a student). - The designer selects exactly one candidate key as the primary key.
- All candidate keys share the same uniqueness and minimality properties; the PK is just the chosen representative.
- The PK columns must not contain NULL values; other candidate keys may optionally be declared UNIQUE NOT NULL.
Referential Integrity (Foreign Key Constraint)
A foreign key value in a child relation must either match a primary key value in the referenced (parent) relation or be NULL. This prevents orphan records. On DELETE or UPDATE of a referenced row, the DBMS can: RESTRICT (block), CASCADE (propagate), SET NULL, or SET DEFAULT.