13.4 KB · commit 40bb182
| title | T03.2 · Relational Keys |
|---|
← 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 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)
Because relational tables do not use physical row numbers, keys identify individual tuples and link related tables.
Superkey: A superkey is a set of one or more attributes whose combined values uniquely identify each tuple in a relation. Formally, for all distinct tuples t1,t2∈r, t1=t2⟹t1[K]=t2[K]. A superkey can include redundant attributes.
Candidate Key: A candidate key is a minimal superkey. It uniquely identifies every tuple, and no proper subset of its attributes can do so. Removing any column destroys uniqueness.
Primary Key: A primary key is the specific candidate key selected by the database designer as the principal row identifier. It must be unique and cannot contain NULL (enforcing Entity Integrity).
Alternate Key (Secondary Key): An alternate key is any candidate key not selected as the primary key. In SQL, alternate keys are enforced using UNIQUE constraints.
Composite Key: A composite key is any key made of two or more attributes working together to identify tuples. It can be a composite superkey, candidate key, or primary key.
Foreign Key: A foreign key is an attribute (or set of attributes) in a child table whose values reference the primary key of a parent table. It enforces referential integrity.
Every primary key is chosen from candidate keys, and every candidate key is a minimal superkey:
Primary Key⊆Candidate Keys⊆Super Keys| Key Type | Plain-English Definition | Minimal? | NULL Allowed? | Max Per Table | Main Purpose |
|---|---|---|---|---|---|
| Super Key | Any set of attributes that uniquely identifies a row. | No (extra columns allowed) | No for identifying columns | Unlimited | Baseline uniqueness. |
| Candidate Key | A minimal super key with no redundant columns. | Yes (strictly minimal) | Yes (unless NOT NULL) | Multiple | Pool of eligible unique identifiers. |
| Primary Key | The single candidate key selected as the main row ID. | Yes | No (never allowed) | Exactly 1 | Principal row identifier (PRIMARY KEY). |
| Alternate Key | Any candidate key not chosen as primary key. | Yes | Yes (unless NOT NULL) | Multiple | Secondary unique identifier (UNIQUE). |
| Composite Key | Any key made of two or more attributes. | Depends on key tier | Depends on key tier | Multiple | Identifies rows in junction tables. |
| Foreign Key | Column(s) referencing the primary key of another table. | Matches parent PK | Yes (if relationship is optional) | Multiple | Links tables and prevents orphan rows. |
Consider relation schema Student(student_id,name,email,nid,dept_name):
| student_id | name | nid | dept_name | |
|---|---|---|---|---|
| 101 | Akib | akib@gmail.com | 1999123456 | ECE |
| 102 | Shuvo | shuvo@gmail.com | 2000654321 | ECE |
| 103 | Sakib | sakib@gmail.com | 2001987654 | ETE |
dept_name is a foreign key referencing Department(dept_name).
Figure: Primary key and foreign key relationship between Student and City tables (Class Notes by Fahim, Page 36).
Foreign keys prevent orphan rows when parent records change. Designers specify automated rules:
ON DELETE CASCADE / ON UPDATE CASCADE: Deleting or updating a parent row automatically deletes or updates matching child rows.ON DELETE SET NULL: Sets child foreign key columns to NULL when the parent row is deleted.RESTRICT / NO ACTION: Blocks deleting or updating a parent record if matching child records exist.Section A · Asked in: 2023 Q.3(b) (4 marks, CLO2) · 2018 Q.2(a) (4 marks) · 2021 Q.2(b) (3 marks, CLO1) · L2 Merge
Question (2023 Q.3(b)). Explain the distinctions among the terms-
(i) Primary key
(ii) Candidate key
(iii) Super key
(iv) Composite key
Question (2018 Q.2(a)). Explain the distinctions among the terms primary key, candidate key, and super key with proper example.
Question (2021 Q.2(b)). Differentiate among Superkey, Candidate Key and Primary Key with an appropriate example.
Answer.
A key is an attribute or set of attributes used to identify rows in a table.
Distinctions Among Keys:
| Key Type | Definition | Minimality | NULL Allowed |
|---|---|---|---|
| (i) Primary Key | The candidate key chosen by the database designer to identify tuples uniquely. | Yes (minimal) | No (never allowed) |
| (ii) Candidate Key | A minimal super key. Removing any attribute destroys uniqueness. | Yes (strictly minimal) | Yes (unless constrained NOT NULL) |
| (iii) Super Key | Any set of attributes that uniquely identifies a row. May contain extra attributes. | No (can have redundant attributes) | No for identifying columns |
| (iv) Composite Key | A key that consists of two or more attributes rather than a single attribute. | Can be minimal (candidate) or non-minimal | Depends on whether it is PK or unique |
Relationship:
Primary Key⊆Candidate Keys⊆Super KeysRunning Example:
Consider relation Student(student_id,name,email,nid,dept_name):
| student_id | name | nid | dept_name | |
|---|---|---|---|---|
| 101 | Akib | akib@gmail.com | 1999123456 | ECE |
| 102 | Shuvo | shuvo@gmail.com | 2000654321 | ECE |
| 103 | Sakib | sakib@gmail.com | 2001987654 | ETE |
Variant for 2018 Q.2(a) (4 marks): Omit (iv) Composite key. Keep the definitions, distinctions table, and the Student running example for Super Key, Candidate Key, and Primary Key.
Variant for 2021 Q.2(b) (3 marks): Same as 2018 Q.2(a). Present the 3-row distinction table and the Student relation key classification.
Section B · Asked in: 2019 Q.6(b) (6 marks) · L1
Question (2019 Q.6(b)). Discuss the different keys in DBMS. Write down the importance and use of foreign key with example.
Answer.
1. Different Keys in DBMS:
2. Importance and Use of Foreign Key:
CASCADE, SET NULL, RESTRICT). When parent records change, child records update or delete automatically.Example:
Consider two relations:
Here, dept_name in Student is a foreign key referencing dept_name in Department.
'ECE', 'ECE' must exist in Department.dept_name = 'Civil', and 'Civil' is not in Department, the DBMS rejects the insert. This protects database integrity.Section A · Asked in: 2020 Q.2(b) (4 marks) · L1
Question (2020 Q.2(b)). Write short note about: Super Key, Candidate Key, Primary Key, and Foreign Key.
Answer.
1. Super Key:
2. Candidate Key:
3. Primary Key:
NOT NULL). It should rarely or never change.4. Foreign Key:
dept_name in Student is a foreign key referencing Department(dept_name).NULL. Candidate keys can theoretically allow NULL unless marked NOT NULL.UNIQUE in the parent table.← Previous: T03.1 · Relation, Tuple, Attribute, and Domain | 🏠 Index | Next: T03.3 · Referential Integrity and Foreign Key Syntax →