27.3 KB · commit 40bb182
← Previous: T03.1: Relation, Tuple, Attribute, and Domain | 🏠 Index | Next: T03.3: Referential Integrity and Foreign Key Syntax →
Topic: T03 — Relational Model, Keys & Integrity | Priority: 🔴 High | Exam Frequency: 5/7 finals (21 marks: 2018 Q2a, 2019 Q6b, 2020 Q2b, 2021 Q2b, 2023 Q3b) Sources: Silberschatz 7e Ch. 2 (§2.3) · Lecture Deck 02 (Slide 7) · Lecture Deck 06 (Slides 11–13) · Class Notes (Raidah p. 5, 7, 13, 19, 20; Fahim p. 36, 46–47)
Relational keys are the most frequently tested topic in Unit 3, appearing in 5 out of 7 RUET semester finals (21 marks total). Since relational tables have no row numbers or pointers, keys are the only way a database can tell two rows apart and link records across different tables. In the exam, you will almost certainly be asked to contrast super keys, candidate keys, primary keys, composite keys, and foreign keys using clear definitions, the subset hierarchy formula, and a small table example.
Superkey: A superkey is a set of one or more attributes whose combined values uniquely identify each tuple in a relation. No two distinct rows in the table can have the same values across all attributes of a superkey.
Candidate Key: A candidate key is a minimal superkey. It uniquely identifies every tuple in a relation, and no proper subset of its attributes is a superkey.
Primary Key: A primary key is the candidate key chosen by the database designer as the main identifier for tuples in a relation. Its attributes must always be unique and can never be NULL.
Alternate Key: An alternate key is any candidate key of a relation that was not chosen by the designer to be the primary key.
Composite Key: A composite key is any key made of two or more attributes that work together to identify tuples in a relation.
Foreign Key: A foreign key is a set of one or more attributes in a referencing (child) relation whose values must match the primary key of a referenced (parent) relation or be NULL.
Imagine trying to identify a specific RUET student out of the entire university:
{roll_no, name, blood_group}, you can find that exact student. Of course, name and blood_group were extra baggage you did not actually need, because roll_no alone was already unique. Any combination of columns that guarantees uniqueness (with or without extra columns) is a super key.{roll_no} and {nid} pass this test on their own. These minimal super keys are your candidate keys (they are the "candidates" competing to become the main ID).{roll_no}) to be the official primary key of the table. The leftover candidate keys that were not picked (like {nid} and {email}) become alternate keys. +--------------------------------------+
| Superkey |
| (Any set that guarantees uniqueness) |
+--------------------------------------+
|
| (Strip away redundant columns)
v
+--------------------------------------+
| Candidate Key |
| (Minimal Superkeys) |
+--------------------------------------+
/ \
/ (Designer picks one) \ (Remaining candidates)
v v
+-----------------------------+ +----------------------------+
| Primary Key | | Alternate Key |
| (Main ID, never NULL) | | (Secondary UNIQUE keys) |
+-----------------------------+ +----------------------------+
Because every primary key is chosen from the candidate keys, and every candidate key is a special (minimal) super key, they form a nested subset chain:
Primary Key⊆Candidate Keys⊆Super Keys| Key Type | Plain-English Definition | Minimal (No Extra Columns)? | NULL Allowed? | Max Per Table | Main Purpose |
|---|---|---|---|---|---|
| Super Key | Any set of columns where no two rows can ever have the exact same values. | No (extra columns allowed) | No for identifying columns | Unlimited | Baseline uniqueness rule. |
| Candidate Key | A minimal super key where no smaller subset of its columns is unique. | Yes (strictly minimal) | Yes (unless marked NOT NULL) | Multiple | Pool of minimal unique identifiers. |
| Primary Key | The single candidate key chosen by the designer as the main row identifier. | Yes | No (never allowed) | Exactly 1 | Official row ID (PRIMARY KEY). |
| Alternate Key | Any candidate key that was not chosen as the primary key. | Yes | Yes (unless marked NOT NULL) | Multiple | Enforced via UNIQUE constraints. |
| Composite Key | Any key made up of two or more columns working together. | Depends on key tier | Depends on key tier | Multiple | Identifies rows in junction tables. |
| Foreign Key | Column(s) in a child table that point to the primary key of a parent table. | Matches parent PK | Yes (if relationship is optional) | Multiple | Links tables & prevents orphan rows. |
Suppose R is the set of all columns in table r(R). A subset of columns K⊆R is a super key if no two distinct rows t1 and t2 can ever agree on all columns of K at the same time. In math notation, that simply means "different rows must have different values on K":
∀t1,t2∈r,t1=t2⟹t1[K]=t2[K]Notice what this rule implies: once a set of columns K is unique, glueing any extra column A onto it (K∪{A}) will still keep it unique! That is why a single column like {ID} can generate dozens of super keys ({ID, name}, {ID, dept_name}, and even the entire row of all columns combined).
A candidate key is a super key with zero wasted columns. It must satisfy two rules:
If you drop even a single column from a candidate key, it immediately loses its ability to uniquely identify rows. Keep in mind that "minimal" does not mean "only one column." If a table requires {student_id, course_id} together to identify a grade, that 2-column pair is minimal because neither column works alone.
The primary key is the single candidate key chosen by the database designer as the main way to identify tuples in the table:
NULL (because you cannot identify a record using an unknown ID).When a table has two or more candidate keys, the designer picks one as the primary key, and all the remaining candidate keys are called alternate keys. In SQL, you still protect alternate keys from duplicates by declaring them with the UNIQUE keyword.
A composite key is not a separate tier in the hierarchy. Instead, it simply means any key (whether super, candidate, primary, or foreign) that is built from two or more attributes rather than a single attribute. For example, {student_id, name} is a composite super key, while {student_id, course_id} in an enrollment table is a composite candidate and primary key.
A foreign key is a column (or set of columns) in a child table whose value must match an existing primary key (or unique candidate key) value in a parent table, or else be NULL:
Figure 1: Primary key and foreign key relationship between Student and City tables (Source: Class Notes by Fahim, Page 36).
Consider the relation schema for students in an academic department:
Student(student_id,nid,email,name,dept_name)| student_id | nid | name | dept_name | |
|---|---|---|---|---|
| 101 | 1999123456 | akib@gmail.com | Akib | ECE |
| 102 | 2000654321 | shuvo@gmail.com | Shuvo | ECE |
| 103 | 2001987654 | sakib@gmail.com | Sakib | ETE |
NULL.'ECE' must exist in the parent Department table.When parent records are deleted or modified, foreign keys prevent dangling (orphan) child records. Database designers configure automated actions using SQL clauses:
CREATE TABLE Student (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
dept_name VARCHAR(20),
FOREIGN KEY (dept_name) REFERENCES Department(dept_name)
ON DELETE CASCADE
ON UPDATE CASCADE
);
'ETE' to 'ECE'), all child records update automatically to reflect the new value.NULL when the parent record is deleted.| Year & Question | Marks | Scenario / Data Given | Key Result / Answer Summary |
|---|---|---|---|
| 2018 Q.2(a) | 4 | Contrast primary, candidate, and super keys with examples | Provide definitions, minimality test, and the Student table example distinguishing the three tiers. |
| 2019 Q.6(b) | 6 | Broad discussion of keys + foreign key importance and use | Full key taxonomy (6 key types) plus 3-mark section on foreign key referential integrity, cascading, and 2-table schema. |
| 2020 Q.2(b) | 4 | Four short notes: Super, Candidate, Primary, Foreign | Structured 1-mark sections per key: Definition, Key Property, and Single-table Example. |
| 2021 Q.2(b) | 3 | Differentiate superkey, candidate key, and primary key | Crisp 3-row distinction table, mathematical subset hierarchy formula, and concrete Student example. |
| 2023 Q.3(b) | 4 | Distinguish primary, candidate, super, and composite keys | Four-part distinction covering definition, minimality, and nullability with Student and Takes examples. |
Appeared: 2023 Q.3(b) — (4 marks)
Question:
Explain the distinctions among the terms-
(i) Primary key
(ii) Candidate key
(iii) Super key
(iv) Composite key
Full Answer:
A key is an attribute or set of attributes used to identify tuples uniquely in a relation.
1. Distinctions Among the Four Keys:
| Key Type | Definition | Minimality Required? | NULL Permitted? |
|---|---|---|---|
| (i) Primary Key | The specific candidate key chosen by the database designer as the main tuple identifier. | Yes (strictly minimal) | No (NOT NULL mandatory) |
| (ii) Candidate Key | A minimal super key where no proper subset can uniquely identify a tuple. | Yes (strictly minimal) | Yes (unless constrained NOT NULL) |
| (iii) Super Key | Any set of attributes that uniquely identifies each tuple. Can contain extra attributes. | No (redundant attributes allowed) | No for identifying columns |
| (iv) Composite Key | Any key that consists of two or more attributes rather than a single attribute. | Can be minimal or non-minimal | Depends on whether it is PK or unique |
2. Hierarchy Formula:
Primary Key⊆Candidate Keys⊆Super Keys3. Illustrative Examples: Consider relation Student(student_id,nid,email,name):
name).NULL.Appeared: 2018 Q.2(a) — (4 marks)
Question:
Explain the distinctions among the terms primary key, candidate key, and super key with proper example.
Full Answer:
1. Conceptual Distinctions:
NULL.2. Relationship:
Primary Key⊆Candidate Keys⊆Super Keys3. Concrete Example:
Consider the relation schema Instructor(ID,name,email,dept_name):
| ID | name | dept_name | |
|---|---|---|---|
| 10101 | Srinivasan | srini@univ.edu | Comp. Sci. |
| 12121 | Wu | wu@univ.edu | Finance |
| 15151 | Mozart | mozart@univ.edu | Music |
Appeared: 2019 Q.6(b) — (6 marks)
Question:
Discuss the different keys in DBMS. Write down the importance and use of foreign key with example.
Full Answer:
Part 1: Different Keys in DBMS
NULL and should never change.Part 2: Importance and Use of Foreign Key
Importance of Foreign Keys:
ON DELETE CASCADE, ON UPDATE CASCADE). Updating or deleting parent records automatically synchronizes child records.Illustrative Example:
Consider two tables:
Here, attribute dept_name in Instructor is a foreign key referencing dept_name in Department.
'Comp. Sci.' exists in Department.'Robotics' does not exist in Department, the DBMS rejects the operation with a referential integrity violation.Appeared: 2020 Q.2(b) — (4 marks)
Question:
Write short note about: Super Key, Candidate Key, Primary Key, and Foreign Key.
Full Answer:
1. Super Key:
2. Candidate Key:
3. Primary Key:
NULL values (NOT NULL constraint).4. Foreign Key:
NULL.Appeared: 2021 Q.2(b) — (3 marks)
Question:
Differentiate among Superkey, Candidate Key and Primary Key with an appropriate example.
Full Answer:
1. Distinctions Table:
| Comparison Point | Superkey | Candidate Key | Primary Key |
|---|---|---|---|
| Definition | Any set of attributes that uniquely identifies a row. | A minimal superkey with no redundant attributes. | The candidate key chosen by the designer to identify records. |
| Minimality | Not required (may have extraneous columns). | Strictly required (irreducible). | Strictly required (minimal). |
| NULL Permitted | No for identifying attributes. | Permitted unless constrained NOT NULL. | Never permitted (NOT NULL enforced). |
| Count Per Table | Can have many. | Can have several. | Exactly one per relation. |
2. Formula:
Primary Key⊆Candidate Keys⊆Superkeys3. Example:
Consider relation schema Student(roll_no,reg_no,name,phone):
name.Source: Silberschatz 7e · Chapter 2 (Practice Exercise 2.1)
Exam Relevance: High-Probability Foundational Exam Drill
Key Tested Concept: Identifying primary keys and candidate keys under varying real-world business constraints.
Problem Statement:
Consider the standard employee relational schema:
- employee(person_name,street,city)
- works(person_name,company_name,salary)
- company(company_name,city)
- manages(person_name,manager_name)
- Specify the primary key for the relation
worksunder the assumption that each person can work for at most one company.- Specify the primary key for
worksif people are permitted to work for multiple companies simultaneously (e.g., part-time consultants).- Specify the primary key for
managesassuming an employee can have at most one manager, versus multiple managers.
Model Exam-Style Solution:
Primary Key of works (Single Employer Assumption):
person_name uniquely identifies the tuple.person_name, there is only one corresponding company_name and salary.Primary Key of works (Multiple Employer Assumption):
person_name can appear in multiple tuples (once for each employer).Primary Key of manages:
person_name maps to a unique manager_name.person_name can be associated with multiple manager_name values.💡 RUET Exam Takeaway: Always state the underlying business assumption when choosing keys. A key is NOT a mathematical accident; it is determined by enterprise policy!
NOT NULL for Primary Keys: Entity integrity forbids NULL in primary keys. Candidate keys and alternate keys can theoretically permit NULL unless explicitly declared NOT NULL.← Previous: T03.1: Relation, Tuple, Attribute, and Domain | 🏠 Index | Next: T03.3: Referential Integrity and Foreign Key Syntax →