G27
Self-checkedTier 1 8 min readdbms.relational-model-keys

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 R(A1,…,An)R(A_1, \ldots, A_n) lists the attributes. The degree (arity) of RR is nn.
  • A relation instance r(R)r(R) is a set of tuples, each tuple a list of nn values, the ii-th from the domain of AiA_i. The cardinality of rr 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 RR with attribute set UU:

  • a superkey is a set K⊆UK \subseteq U such that no two tuples of any legal instance have equal values on all of KK;
  • 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 FF of R1R_1 is a foreign key referencing R2R_2 if its values in tuples of R1R_1 must be either NULL or equal to the values of the candidate (usually primary) key of some tuple of R2R_2. 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 R(A,B,C)R(A, B, C) with candidate key {A}\{A\} the superkeys are the subsets that contain AA: {A},{A,B},{A,C},{A,B,C}\{A\}, \{A, B\}, \{A, C\}, \{A, B, C\}, four of them. With one candidate key of size kk among nn attributes there are 2n−k2^{n-k} superkeys (each of the other n−kn - k 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
schemasuperkeysall 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.

0 / 6
computed by script: relational operators are evaluated by scripts/db_lib.py (and cross-checked against SQLite), one step at a time. python3 scripts/gen-db1-traces.py

For example, in R(A,B,C,D)R(A, B, C, D) with candidate keys {A,B}\{A, B\} and {B,C}\{B, C\}: 44 superkeys contain {A,B}\{A, B\}, 44 contain {B,C}\{B, C\}, and 22 contain both (they contain {A,B,C}\{A, B, C\}), so 4+4−2=64 + 4 - 2 = 6. If every single attribute is a candidate key (four attributes) every non-empty subset is a superkey: 24−1=152^4 - 1 = 15.

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 nn attributes is at most (n⌊n/2⌋)\binom{n}{\lfloor n/2 \rfloor} (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
Dept, Emp (Emp.did references Dept.did)
Dept2 tuples
diddname
10Sales
20HR
Emp3 tuples
eidenamedid
1Ravi10
2Sana10
3Tara20

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).

0 / 4
computed by script: relational operators are evaluated by scripts/db_lib.py (and cross-checked against SQLite), one step at a time. python3 scripts/gen-db1-traces.py

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 KEY implies UNIQUE and NOT NULL (SQLite is a known exception for non-integer columns); UNIQUE columns may contain NULLs (several of them, in most systems) and serve as alternate keys.
  • Check from the instance: if two tuples agree on KK, then KK is not a key. Example: Student(sid, sname, dept) with tuples (1, Asha, CS) and (3, Chen, CS): {dept}\{\text{dept}\} is not a key; {sid}\{\text{sid}\} may be.

Worked examples

EX 1Superkeys of a small schema

Easy

R(A,B,C)R(A, B, C) with the single candidate key {A}\{A\}: superkeys AA, ABAB, ACAC, ABCABC (four). With candidate keys {A}\{A\} and {B}\{B\}: the sets containing AA (four) or BB (four) minus those containing both (two): six.

EX 2Refute a key from an instance

Easy

Student: (1, Asha, CS), (2, Bilal, EE), (3, Chen, CS), (4, Dara, ME). {dept}\{\text{dept}\} is not a key (CS twice). {sname}\{\text{sname}\} and {sid}\{\text{sid}\} 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

Medium

R(A,B,C,D,E)R(A, B, C, D, E) with candidate keys {A}\{A\} and {B,C}\{B, C\}. Superkeys containing AA: 24=162^4 = 16. Containing {B,C}\{B, C\}: 23=82^3 = 8. Containing both AA and {B,C}\{B, C\}: 22=42^2 = 4. Total 16+8−4=2016 + 8 - 4 = 20 (the table lists 2020).

EX 4Referential integrity

Medium

Emp(eid, ename, did) with did→Dept(did)\text{did} \to \text{Dept}(\text{did}). 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 nn attributes whose only candidate key has kk attributes has how many superkeys?" 2n−k2^{n-k}. (b) "The number of superkeys of R(A,B,C,D)R(A, B, C, D) if ABAB and BCBC are the candidate keys." 66. (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 kk in nn attributes: 2n−k2^{n-k} 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

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

year not read yetQ3 · page 369 of the book
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)
GATE 2023 · MCQQ10 · page 370 of the book
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.
GATE 2017 · Set 2 · NAT · 1 markQ18 · page 371 of the book
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 _____.
GATE 2016 · Set 1 · MCQQ21 · page 372 of the book
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
GATE 2014 · Set 2 · NATQ26 · page 373 of the book
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______
GATE 2011 · MCQ · 1 markQ30 · page 373 of the book
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
GATE 2012 · MCQ · 1 markQ7 · page 385 of the book
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
GATE 2005 · MCQQ12 · page 386 of the book
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)
GATE 2021 · Set 2 · MCQQ7 · page 389 of the book
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.
GATE 2012 · MCQ · 2 marksQ18 · page 391 of the book
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 R(A,B,C,D)R(A, B, C, D) have if {A}\{A\} is its only candidate key?

Show answer

24−1=82^{4 - 1} = 8.

Question 2

Can the instance {(1, x), (2, x)} prove that the first attribute is a key?

Show answer

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 answer

The employees remain; their department attribute becomes NULL.

Question 4

Which operations can violate referential integrity?

Show answer

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.
  • 2n−k2^{n-k} superkeys for one key of size kk; 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