Databases (DBMS)
Relational Model
Relations are the data structure behind every query: learn the formal vocabulary, the key hierarchy, how to count super keys, and what integrity constraints and views guarantee.
GATE CSE relational model — relations, tuples, domains, super/candidate/primary/foreign keys, counting super keys, entity and referential integrity, referential actions and views, with sample questions, flashcards and drills.
Learning outcomes
- Use schema, instance, degree, cardinality, domain and NULL correctly
- Distinguish super, candidate, primary, alternate and foreign keys
- Count super keys given the candidate keys
- Predict the effect of inserts, deletes and updates under entity and referential integrity
Relations, tuples, schema and instance
- A RELATION SCHEMA R(A1, A2, ..., An) lists the attribute names (each with a DOMAIN, the set of allowed atomic values). A RELATION INSTANCE is a finite SET of tuples; each tuple is an ordered list of values, one per attribute.
- Because a relation is a SET: tuples are unordered, there are NO duplicate tuples, and the ordering of attributes does not matter (in the formal model). SQL tables, in contrast, may hold duplicates (they are multisets) unless a key is declared.
- DEGREE (arity) = number of attributes. CARDINALITY = number of tuples. For a relation over domains D1,...,Dn the largest possible instance has |D1| * ... * |Dn| tuples.
- NULL represents an unknown or inapplicable value; it is not a member of any domain and NULL is not equal to NULL (three-valued logic: TRUE, FALSE, UNKNOWN).
- Database SCHEMA = set of relation schemas + integrity constraints; the DATABASE STATE (instance) changes with every insert, delete and update.
- Number of possible relation instances over a domain-product of size N is 2^N (each tuple is either present or absent).
Formulas to remember
-
Maximum tuples in a relation = product of the domain sizes -
Number of possible relation instances = 2^(maximum tuples) -
Cartesian product R x S: degree = deg(R) + deg(S), cardinality = |R| * |S|
| Term | Meaning | Changes over time? |
|---|---|---|
| Domain | set of allowed atomic values of an attribute | no |
| Relation schema R(A1..An) | attribute names + domains (+ constraints) | rarely |
| Relation instance | finite set of tuples currently stored | with every insert, delete, update |
| Degree (arity) | number of attributes | only when the schema changes |
| Cardinality | number of tuples | yes |
| Database state | set of instances of all relations | yes |
Worked example
Problem. R(A, B) with |dom(A)| = 3 and |dom(B)| = 4. How many tuples can an instance of R have at most, and how many different instances exist?
Solution. At most 3 * 4 = 12 tuples. Every subset of the 12 possible tuples is a valid instance, so there are 2^12 = 4096 instances.
Keys and counting super keys
- SUPER KEY: a set of attributes that uniquely identifies every tuple. Any superset of a super key is a super key. CANDIDATE KEY: a MINIMAL super key (no proper subset is a super key). PRIMARY KEY: the candidate key chosen by the designer (never NULL). ALTERNATE KEY: a candidate key that was not chosen. PRIME ATTRIBUTE: belongs to some candidate key.
- FOREIGN KEY: attributes in R1 whose values must match the primary (or a candidate/unique) key of R2 or be NULL. The referenced key must be a candidate key of the referenced relation.
- A relation always has at least one candidate key (the set of all attributes is a super key). The number of candidate keys can be many; they can overlap and have different sizes.
- COUNTING SUPER KEYS: if the relation has n attributes and exactly one candidate key of size k, the number of super keys is 2^(n - k). With several candidate keys, count subsets that contain at least one candidate key (use inclusion-exclusion).
- For n attributes, the maximum number of candidate keys is C(n, floor(n/2)) because candidate keys form an antichain (none is a subset of another).
- A COMPOSITE key has more than one attribute. A SURROGATE key is a system-generated artificial key.
Formulas to remember
-
One candidate key of size k in n attributes: super keys = 2^(n - k) -
Two candidate keys K1, K2: super keys = 2^(n-|K1|) + 2^(n-|K2|) - 2^(n-|K1 U K2|) -
Maximum number of candidate keys = C(n, floor(n/2))
Worked example
Problem. R(A, B, C, D) has candidate keys {A} and {B, C}. How many super keys does R have?
Solution. Super keys containing A: 2^3 = 8. Containing {B, C}: 2^2 = 4. Containing both A and {B, C}: 2^1 = 2. Total = 8 + 4 - 2 = 10. (List: eight sets containing A, plus BC and BCD.)
Counting super keys and instances: worked patterns
- To count super keys when the candidate keys are known, count the subsets of the n attributes that CONTAIN at least one candidate key. For one key of size k the answer is 2^(n-k). For several keys use inclusion-exclusion over the unions of the keys.
- For three keys K1, K2, K3: |S| = sum 2^(n - |Ki|) - sum 2^(n - |Ki U Kj|) + 2^(n - |K1 U K2 U K3|). Always compute the SIZE OF EACH UNION, not the sum of sizes.
- If every attribute set of size m is a candidate key (all m-subsets of n attributes), the super keys are exactly the subsets of size >= m: sum of C(n, j) for j = m..n.
- For the number of instances of a relation that must satisfy a KEY constraint, count choices per key value. If key A has a values and the non-key part has b combinations, each key value is either absent or paired with one of the b combinations: (b + 1)^a instances.
- The number of instances with EXACTLY t tuples (no constraint) over a domain-product of size N is C(N, t); with at most t tuples it is the sum of C(N, j) for j = 0..t.
- NATURAL JOIN-style size questions in this chapter always ask for a BOUND: minimum is 0 (no match) unless a foreign key forces matches, maximum is the product of the counts for the matching value.
Formulas to remember
-
k-subsets of n attributes all candidate keys: super keys = C(n, k) + C(n, k+1) + ... + C(n, n) -
Instances with key A (a values), b combinations for the rest: (b + 1)^a -
Instances with exactly t tuples over domain-product N: C(N, t)
| R(n attributes), candidate keys | Super keys | Working |
|---|---|---|
| R(A,B,C,D,E), only {A} | 16 | 2^(5-1) |
| R(A,B,C,D,E), {A} and {B,C} | 20 | 16 + 8 - 4 (union ABC has size 3) |
| R(A,B,C,D), {A,B} and {B,C} | 6 | 4 + 4 - 2 (union ABC) |
| R(A,B,C), {A,B}, {A,C}, {B,C} | 4 | 2+2+2 - 1-1-1 + 1 |
| R(A,B,C), only {A,B,C} | 1 | 2^0 |
| Maximum candidate keys, n = 4 | 6 keys | C(4, 2) |
Worked example
Problem. R(A, B) has A as its key. dom(A) has 3 values and dom(B) has 2 values. How many different valid instances (including the empty relation) can R have? How many have exactly 2 tuples?
Solution. Each of the 3 values of A is either absent or appears once with one of the 2 values of B: 3 choices per A value, so 3^3 = 27 instances (this equals (b + 1)^a = 3^3). Exactly 2 tuples: pick which 2 of the 3 A values appear, C(3, 2) = 3, and a B value for each: 2 * 2 = 4, giving 3 * 4 = 12 instances. Without the key constraint, exactly 2 tuples over 6 possible tuples would give C(6, 2) = 15.
Integrity constraints, referential actions and views
- DOMAIN constraint: a value must come from the declared type/check. KEY constraint: no two tuples agree on a candidate key. ENTITY INTEGRITY: no attribute of the primary key may be NULL. REFERENTIAL INTEGRITY: every non-NULL foreign-key value must exist as a key value in the referenced relation.
- A foreign key MAY be NULL (unless declared NOT NULL) and may refer to the same relation (self-reference). Several tuples may share the same foreign-key value (many-to-one).
- Violations: INSERT into the referencing relation with a missing parent value is rejected; INSERT into the referenced relation never violates referential integrity. DELETE or UPDATE of a referenced key can violate it.
- REFERENTIAL ACTIONS on delete/update of the parent: NO ACTION / RESTRICT (reject), CASCADE (delete or update the children too), SET NULL (children get NULL), SET DEFAULT (children get the default). Cascades can chain through several relations.
- A VIEW is a stored query: a virtual relation that holds no data of its own (a MATERIALIZED view does store the result and must be refreshed). Views provide security (hide columns/rows), simplify queries and give logical data independence.
- Updates through a view are allowed only when the view maps unambiguously to one base table (typically a simple SELECT without aggregates, DISTINCT or joins and including the key). Views on joins or aggregates are generally not updatable.
- LOGICAL data independence = changing the conceptual schema without changing external views/applications; PHYSICAL data independence = changing storage structures without affecting the logical schema.
| Operation | May violate | Typical response |
|---|---|---|
| INSERT into referencing table | referential integrity (bad foreign key) | reject |
| INSERT into referenced table | entity/key integrity only (duplicate or NULL key) | reject |
| DELETE from referenced table | referential integrity | reject / CASCADE / SET NULL |
| DELETE from referencing table | nothing | allowed |
| UPDATE of a key in referenced table | referential integrity | reject / CASCADE |
Worked example
Problem. Emp(eid, did) has a foreign key did referencing Dept(did) with ON DELETE CASCADE. Dept has 3 tuples (1, 2, 3) and Emp has employees with did = 1, 1, 2, 3, 3, 3. After DELETE FROM Dept WHERE did = 3, how many tuples remain in Emp?
Solution. CASCADE removes every Emp tuple that references did = 3, i.e. 3 tuples. Remaining Emp tuples = 6 - 3 = 3.
Constraint checking and cascades: worked cases
- For every operation ask: which table is it on, and which constraint can it break? INSERT on the CHILD: foreign key. INSERT on the PARENT: only key/entity integrity. DELETE on the PARENT: foreign key. DELETE on the CHILD: nothing. UPDATE of the parent key: foreign key; UPDATE of the child foreign-key value: foreign key.
- ENTITY integrity concerns the primary key (never NULL, never duplicate). A UNIQUE constraint rejects duplicates but most systems accept several NULLs. A CHECK constraint accepts a row when the condition is TRUE or UNKNOWN.
- CASCADE chains: if A references B and B references C, with CASCADE on both, deleting a row of C may delete rows of B and then rows of A. Count rows by following the chain level by level; with SET NULL the chain stops because the child row is kept.
- A SELF-REFERENCING foreign key (Emp.mgr references Emp.eid) with ON DELETE CASCADE deletes the whole subtree below the deleted employee. With NO ACTION the delete is rejected while subordinates exist.
- DEFERRED constraint checking lets a transaction violate a constraint temporarily and verifies it at COMMIT, which is needed to insert two tuples that reference each other.
- A foreign key may reference only a candidate key (UNIQUE or PRIMARY KEY columns) of the parent. A composite foreign key must match all columns of the composite key; MATCH SIMPLE accepts a partly-NULL foreign key.
| Operation | Constraint that may fail | NO ACTION | CASCADE | SET NULL |
|---|---|---|---|---|
| INSERT child with missing parent | foreign key | rejected | rejected | rejected |
| DELETE parent with 2 children | foreign key | rejected | parent and 2 children deleted | parent deleted, 2 FKs become NULL |
| UPDATE parent key (referenced) | foreign key | rejected | children keys updated too | children FK set to NULL |
| INSERT parent with duplicate key | key / entity integrity | rejected | rejected | rejected |
Worked example
Problem. Emp(eid, mgr) has mgr as a foreign key to Emp(eid) with ON DELETE CASCADE. Rows (eid, mgr): (1, NULL), (2, 1), (3, 1), (4, 2), (5, 4), (6, 3). How many rows remain after DELETE FROM Emp WHERE eid = 2?
Solution. Deleting employee 2 cascades to every row whose mgr is 2: row (4, 2). Deleting 4 cascades to rows whose mgr is 4: row (5, 4). Nobody reports to 5, so the chain stops. Rows deleted: 2, 4, 5 = 3 rows. Remaining = 6 - 3 = 3 rows: (1, NULL), (3, 1), (6, 3). The branch under employee 3 is untouched.
Exam tips
- Super keys with a single candidate key of size k out of n attributes: 2^(n-k). With two keys use inclusion-exclusion.
- Inserting into the child table or deleting from the parent are the dangerous operations for referential integrity.
- A foreign key can be NULL and can reference any candidate (unique) key, not only the primary key.
- Primary key attributes are never NULL; a UNIQUE column may hold NULLs (in most systems).
- A view stores no data unless it is materialized, and most views over joins/aggregates are not updatable.
8 GATE-style MCQs for Relational Model with full explanations. Attempt each question before revealing the answer.
R(A, B, C, D) has A as its only candidate key. How many super keys does R have?
Show answer & explanation
Answer: C
Any subset that contains A is a super key: 2^(4-1) = 8 subsets.
R(A, B, C, D) has candidate keys {A} and {B, C}. The number of super keys is:
Show answer & explanation
Answer: B
8 (contain A) + 4 (contain BC) - 2 (contain A and BC) = 10.
Which one of the following statements about foreign keys is FALSE?
Show answer & explanation
Answer: C
A foreign key is an ordinary column(s) that need not belong to the primary key of its own relation.
Emp(eid, did) with a foreign key did referencing Dept(did) and NO ACTION on delete. Which operation is rejected?
Show answer & explanation
Answer: C
Deleting a referenced parent would leave dangling foreign keys. The other operations are valid.
R(A, B) with |dom(A)| = 3 and |dom(B)| = 4. The maximum number of tuples in an instance of R is:
Show answer & explanation
Answer: B
The set of all possible tuples is the Cartesian product of the domains: 3 * 4 = 12. (4096 = 2^12 is the number of instances.)
R has 4 attributes and 5 tuples; S has 3 attributes and 6 tuples. The Cartesian product R x S has:
Show answer & explanation
Answer: A
Degrees add (4 + 3 = 7) and tuple counts multiply (5 * 6 = 30).
Entity integrity requires that:
Show answer & explanation
Answer: B
Entity integrity: primary-key attributes can never be NULL, so every tuple is identifiable.
Which statement about views is correct?
Show answer & explanation
Answer: C
A standard view is a stored query, not stored data. Views can be built on other views, and only simple views are updatable.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
R(A, B, C) has candidate keys {A} and {B}. The number of super keys is:
Show answer & explanation
Answer: C) 6
Super keys containing A: 4; containing B: 4; both: 2. 4 + 4 - 2 = 6 (A, AB, AC, ABC, B, BC).
The maximum number of candidate keys possible for a relation with 4 attributes is:
Show answer & explanation
Answer: B) 6
Candidate keys form an antichain, so the maximum is C(4, 2) = 6 (all two-attribute subsets).
Dept(did) has tuples 1, 2, 3 and Emp(eid, did) references it with ON DELETE SET NULL. After deleting Dept tuple 2 which was referenced by 4 employees, the number of Emp tuples with did = NULL is:
Show answer & explanation
Answer: C) 4
SET NULL sets the foreign key of all four referencing tuples to NULL; no tuple is deleted.
Which one of the following can never violate referential integrity?
Show answer & explanation
Answer: C) inserting into the referenced relation
A new parent row cannot make an existing child reference invalid.
A relation R has 3 attributes whose domains have 2, 3 and 2 values. The number of distinct relation instances (including the empty one) is:
Show answer & explanation
Answer: C) 2^12
There are 2 * 3 * 2 = 12 possible tuples, and every subset is an instance: 2^12.
Logical data independence refers to the ability to:
Show answer & explanation
Answer: B) change the conceptual schema without changing external views or application programs
Physical data independence deals with storage; logical data independence is about the conceptual level.
In R(A, B, C, D) with the single candidate key {A, B}, the number of super keys is:
Show answer & explanation
Answer: B) 4
Super keys must contain AB: 2^(4-2) = 4 (AB, ABC, ABD, ABCD).
Which of the following is NOT true for a relation in the formal relational model?
Show answer & explanation
Answer: D) the order of tuples defines the primary key
Tuples are unordered; keys are defined on attribute values, never on tuple position.
Topic FAQs
What is the difference between a candidate key and a super key?
A super key is any attribute set that identifies tuples uniquely. A candidate key is a super key with no proper subset that is also a super key (minimal).
Can a foreign key be NULL?
Yes, unless the column is declared NOT NULL or is part of a primary key. A NULL foreign key simply means "no related parent".
Does inserting into the parent table break referential integrity?
No. New parent tuples cannot be referenced by missing children, so only key/entity violations (duplicate or NULL key) are possible there.
Why is a relation different from an SQL table?
A relation is a set: no duplicates and no ordering of tuples. An SQL table is a multiset unless a key or DISTINCT removes duplicates.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.