Relational model and keys
The relational model: relation schemas and instances, tuples, domains, degree and cardinality, why a relation is a set; superkeys, candidate keys, primary keys, prime attributes and alternate keys, counting superkeys; foreign keys and referential integrity with the referential actions (cascade, set null, restrict) run on a real database engine, entity integrity, NULL values, and the difference between a relation and a SQL table.
Why it exists
Every later topic of the course (algebra, SQL, normalisation) talks about relations, tuples and keys. Keys are the most examined part: count the superkeys, find the candidate keys, say what happens when a referenced tuple is deleted. This page fixes the vocabulary.
Prerequisites
Relations are sets of tuples; the notation comes from the mathematics of relations (set theory course).
Precise definitions
- A domain is a set of atomic values (integers, strings, dates). An attribute is a name with a domain.
- A relation schema lists the attributes. The degree (arity) of is .
- A relation instance is a set of tuples, each tuple a list of values, the -th from the domain of . The cardinality of is the number of tuples.
- Because a relation is a set, it has no duplicate tuples and the order of tuples and of attributes does not matter (attributes are identified by name). A SQL table is a bag: it may contain duplicates unless a key forbids them.
- Values are atomic (first normal form). A special value NULL stands for unknown or inapplicable; any comparison with NULL is neither true nor false but unknown, and a NULL never matches anything in a join.
Keys. For a relation schema with attribute set :
- a superkey is a set such that no two tuples of any legal instance have equal values on all of ;
- a candidate key is a minimal superkey (no proper subset is a superkey);
- the primary key is the candidate key chosen by the designer (it may not be NULL); the other candidate keys are alternate keys;
- an attribute is prime if it belongs to some candidate key, otherwise non-prime.
Foreign key. A set of attributes of is a foreign key referencing if its values in tuples of must be either NULL or equal to the values of the candidate (usually primary) key of some tuple of . Referential integrity is this requirement. Entity integrity: no attribute of a primary key may be NULL.
A key is a statement about all legal instances (a constraint on the schema), not about the current instance: an instance can show that a set is not a key (two tuples agree), but it can never prove that it is.
How it works
Superkeys and candidate keys
For with candidate key the superkeys are the subsets that contain : , four of them. With one candidate key of size among attributes there are superkeys (each of the other attributes may or may not be added). With several candidate keys count by inclusion–exclusion: the superkeys are the sets that contain at least one candidate key.
Counting superkeys: start
Computed by a script, not typed| schema | superkeys | all attribute subsets |
|---|---|---|
| R(A, B, C), key {A} | ||
| R(A, B, C), keys {A}, {B} | ||
| R(A, B, C, D), key {A, B} | ||
| R(A, B, C, D), keys {A, B}, {B, C} | ||
| R(A, B, C, D, E), keys {A}, {B, C} | ||
| R(A, B, C, D), keys {A}, {B}, {C}, {D} |
Step 0
A superkey is any set of attributes that contains a candidate key. The counts come from listing every subset and testing whether it contains a candidate key.
For example, in with candidate keys and : superkeys contain , contain , and contain both (they contain ), so . If every single attribute is a candidate key (four attributes) every non-empty subset is a superkey: .
Facts used constantly.
- The set of all attributes is always a superkey (rows are distinct in a relation).
- A candidate key is never a proper subset of another candidate key.
- The number of candidate keys of a schema with attributes is at most (no candidate key contains another, so they form an antichain of subsets; Sperner's theorem, quoted).
- Different candidate keys may have different sizes.
Foreign keys and referential actions
When a tuple of the referenced relation is deleted (or its key updated) while other tuples refer to it, the system applies the referential action chosen for the foreign key:
- CASCADE: delete (update) the referencing tuples too;
- SET NULL (and SET DEFAULT): put NULL (the default) in the foreign key of the referencing tuples;
- RESTRICT / NO ACTION: refuse the operation.
These were executed on a real database engine (SQLite with foreign keys switched on). Employees reference departments; the department 10 (Sales) is deleted under each action:
Referential actions of a foreign key: start
Computed, not typed| did | dname |
|---|---|
| 10 | Sales |
| 20 | HR |
| eid | ename | did |
|---|---|---|
| 1 | Ravi | 10 |
| 2 | Sana | 10 |
| 3 | Tara | 20 |
Step 0
Emp.did is a foreign key referencing Dept.did. We now delete the department 10 (Sales) under four different referential actions, using a real database engine (SQLite with foreign keys switched on).
Two employees worked in Sales: with CASCADE they disappear, with SET NULL they stay with no department, with RESTRICT the delete is refused and nothing changes ("FOREIGN KEY constraint failed").
A foreign key may be NULL (unless declared NOT NULL), may reference the same relation (employees and their managers) and may be part of the primary key (a weak entity's key contains the owner's key). It must reference a candidate key.
Keys in practice
- A key chosen from real data (a name, a date) is risky: it must stay unique for all future instances. Designers use surrogate keys (an artificial id).
- In standard SQL,
PRIMARY KEYimpliesUNIQUEandNOT NULL(SQLite is a known exception for non-integer columns);UNIQUEcolumns may contain NULLs (several of them, in most systems) and serve as alternate keys. - Check from the instance: if two tuples agree on , then is not a key. Example: Student(sid, sname, dept) with tuples (1, Asha, CS) and (3, Chen, CS): is not a key; may be.
Worked examples
EX 1Superkeys of a small schema
Easywith the single candidate key : superkeys , , , (four). With candidate keys and : the sets containing (four) or (four) minus those containing both (two): six.
EX 2Refute a key from an instance
EasyStudent: (1, Asha, CS), (2, Bilal, EE), (3, Chen, CS), (4, Dara, ME). is not a key (CS twice). and are consistent with the instance, but the instance alone does not prove that either is a key: the schema's constraint decides.
EX 3Count superkeys with two candidate keys
Mediumwith candidate keys and . Superkeys containing : . Containing : . Containing both and : . Total (the table lists ).
EX 4Referential integrity
MediumEmp(eid, ename, did) with . Inserting (4, Uma, 99) when no department 99 exists is refused. Inserting (4, Uma, NULL) is allowed. Deleting Dept 10 with ON DELETE CASCADE removes the employees of department 10; the animation shows the result.
EX 5Typical questions
GATE level(a) "A relation with attributes whose only candidate key has attributes has how many superkeys?" . (b) "The number of superkeys of if and are the candidate keys." . (c) "Can a primary key contain NULL?" No. (d) "Can a foreign key contain NULL?" Yes unless declared NOT NULL. (e) "Must a foreign key reference a primary key?" It must reference a candidate key (usually the primary key). (f) "Is every superkey a candidate key?" No, only the minimal ones.
Cheat box
Cheat box
- Relation = set of tuples: no duplicates, no order. SQL table = bag.
- Superkey: contains a candidate key. Candidate key: minimal superkey. Primary key: chosen one, not NULL.
- One candidate key of size in attributes: superkeys. Several keys: inclusion–exclusion.
- Foreign key: NULL or an existing key value. Actions: CASCADE, SET NULL/DEFAULT, RESTRICT/NO ACTION.
- An instance can refute a key, never prove it.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
No cross-subject links yet.
Keys come from the ER design (ER model, ER to relational mapping); relational algebra operates on these relations (relational algebra); functional dependencies generalise keys (functional dependencies).
PYQs
10 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 5 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider a relational database schema with a relation R(A,
B, C, D). If {A, B} and {A, C} are the only two candidate
keys of the relation R, then the number of super keys of
relation R is ______ (answer in integer)Which one of the options given below refers to the degree (or arity) of a relation in relational database systems?
- (a)Number of attributes of its relation schema.
- (b)Number of tuples stored in the relation.
- (c)Number of entries in the relation.
- (d)Number of distinct domains of its relation schema.
Consider the following tables T1 and T2. In table T1, P is the primary key and Q is the foreign key referencing R in table T2 with on-delete cascade. In table T2, R is the primary key and S is the foreign key referencing P in table T1 with on-delete set NULL and on- update cascade. In order to delete record (3, 8) from table T1, the number of additional records that need to be deleted from table T1 is _____.
Which of the following is NOT a super key in a relational schema with attributes V, W, X, Y ,Z and primary key VY?
- (a)VXYZ
- (b)VWXZ
- (c)VWXY
- (d)VWXYZ
Given an instance of the STUDENTS relation as shown below. For (StudentName, StudentAge) to be the key for this instance, the value X should NOT be equal to______
Consider a relational table with a single record for each registered student with the following attributes. 1. Registration_ Num: Unique registration number of each registered student 2. UID: Unique identity number, unique at the national level for each citizen 3. BankAccount_Num: Unique account number at the bank. A student can have multiple accounts or join accounts. This attribute stores the primary account number. 4. Name: Name of the student 5. Hostel _ Room: Room number of the hostel Which of the following options is INCORRECT?
- (a)Bank Account _ Num is a candidate key.
- (b)Registration _ Num can be a primary key.
- (c)UID is a candidate key if all students are from the same country.
- (d)If S is a super key such that S∩UID is NULL then S ∪ UID is also a super key. Student ID Student Name Student Email Student Age CPI 2345 Shankar shankar@mat h X 9.4 1287 Swati swati@ee 19 9.5 7853 Shankar shankar@cse 19 9.4 9876 Swati swati@mech 18 9.3 8765 Ganesh ganesh@civil 19 8.7 DATABASE MANAGEMENT SYSTEM
Given the basic ER and relational models, which of the following is INCORRECT?
- (a)An attribute of an entity can have more than one value
- (b)An attribute of an entity can be composite
- (c)In a row of a relational table, an attribute can have more than one value
- (d)In a row of a relational table, an attribute can have exactly one value or a NULL value
The following table has two attributes A and C where A is the primary key and C is the foreign key referencing A with ON DELETE CASCADE. The set of all tuples that must be additionally deleted to preserve referential integrity when the tuple (2, 4) is deleted is:
- (a)(3, 4) and (6, 4)
- (b)(5, 2) and (7, 2)
- (c)(5, 2), (7, 2) and (9, 5)
- (d)(3, 4), (4, 3) and (6, 4)
Consider the following statements S1 and S2 about the relational data model: S1: A relation scheme can have at most one foreign key. S2: A foreign key in a relation scheme R cannot be used to refer to tuples of R. Which one of the following choices is correct?
- (a)S1 is false and S2 is true.
- (b)S1 is true and S2 is false.
- (c)Both S1 and S2 are false.
- (d)Both S1 and S2 are true.
Suppose R1(A, B) and R2(C, D) are two relation schemas. Let r1 and r2 be the corresponding relation instances. B is a foreign key that refers to C in R2. If data in r1 and r2 satisfy referential integrity constraints, which of the following is ALWAYS TRUE? DATABASE MANAGEMENT SYSTEM
- (a)ΠB(r1) – Πc(r2) = ∅
- (b)ΠC(r2) – ΠB(r1) = ∅
- (c)ΠB(r1) = Πc(r2)
- (d)ΠB(r1) – Πc(r2) ≠ ∅
Self-check
Question 1
How many superkeys does have if is its only candidate key?
Show answerHide
.
Question 2
Can the instance {(1, x), (2, x)} prove that the first attribute is a key?
Show answerHide
No. It is consistent with it, but a key is a constraint on all legal instances. (It does show that the second attribute is not a key.)
Question 3
What happens under ON DELETE SET NULL when a referenced department is deleted?
Show answerHide
The employees remain; their department attribute becomes NULL.
Question 4
Which operations can violate referential integrity?
Show answerHide
Insert into the referencing relation, delete from the referenced relation (and updates of the key attributes on either side).
Revision card
One-screen revision
- Relation = set of tuples; degree = number of attributes; cardinality = number of tuples.
- Superkey / candidate key / primary key; prime attributes.
- superkeys for one key of size ; inclusion–exclusion otherwise.
- Foreign key + actions: CASCADE, SET NULL, RESTRICT.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 2 (introduction to the relational model)
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 5 (the relational data model and relational database constraints)
Verification
scripts/enum-checks/db_algebra.py: the number of superkeys of the six example schemas was recounted by inclusion-exclusion over the candidate keys and equals the enumeration; for a single candidate key of size k among n attributes the number of superkeys is 2^(n-k) (n up to 6); the referential actions were executed on a real SQLite database (foreign keys switched on) in a fresh database per action and the resulting Emp relation equals the one in the animation (cascade deletes the referencing tuples, set null and set default give NULL, restrict refuses the delete with 'FOREIGN KEY constraint failed'). The NULL rules (a NULL never joins) follow SQL and are the behaviour of SQLite. Remarks on SQL semantics that differ between database systems (for example the default for NO ACTION being checked at the end of the statement) are textbook knowledge, quoted. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)