Skip to main content

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.

~3 hrs ~0.5–1 marks · Moderate 8 samples · 8 practice · 8 cards

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|
TermMeaningChanges over time?
Domainset of allowed atomic values of an attributeno
Relation schema R(A1..An)attribute names + domains (+ constraints)rarely
Relation instancefinite set of tuples currently storedwith every insert, delete, update
Degree (arity)number of attributesonly when the schema changes
Cardinalitynumber of tuplesyes
Database stateset of instances of all relationsyes

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.

Student relation drawn as a table: the header row is the schema with degree 3 and the data rows are the instance with cardinality 3, plus formulas for maximum tuples and number of instances.
Schema is the header (intension), instance is the current set of rows (extension). Counting facts: max tuples = product of domain sizes, instances = 2^(max tuples).

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

Nested boxes showing the primary key inside the candidate keys inside the super keys, with a Student example and a foreign key arrow to Dept.
Key hierarchy: primary key is one of the candidate keys, every candidate key is a minimal super key, and every superset of a super key is again a super key.

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 keysSuper keysWorking
R(A,B,C,D,E), only {A}162^(5-1)
R(A,B,C,D,E), {A} and {B,C}2016 + 8 - 4 (union ABC has size 3)
R(A,B,C,D), {A,B} and {B,C}64 + 4 - 2 (union ABC)
R(A,B,C), {A,B}, {A,C}, {B,C}42+2+2 - 1-1-1 + 1
R(A,B,C), only {A,B,C}12^0
Maximum candidate keys, n = 46 keysC(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.
OperationMay violateTypical response
INSERT into referencing tablereferential integrity (bad foreign key)reject
INSERT into referenced tableentity/key integrity only (duplicate or NULL key)reject
DELETE from referenced tablereferential integrityreject / CASCADE / SET NULL
DELETE from referencing tablenothingallowed
UPDATE of a key in referenced tablereferential integrityreject / 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.

Dept parent table and Emp child table linked by a foreign key, with the operations that can violate referential integrity, the always-safe operations, and the four referential actions.
Referential integrity: only inserts/updates on the child and deletes/updates on the parent can violate it; the ON DELETE action decides whether the delete is rejected, cascaded or nulled.

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.
OperationConstraint that may failNO ACTIONCASCADESET NULL
INSERT child with missing parentforeign keyrejectedrejectedrejected
DELETE parent with 2 childrenforeign keyrejectedparent and 2 children deletedparent deleted, 2 FKs become NULL
UPDATE parent key (referenced)foreign keyrejectedchildren keys updated toochildren FK set to NULL
INSERT parent with duplicate keykey / entity integrityrejectedrejectedrejected

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.
Exam pattern · 8 questions

GATE sample questions & answers

8 GATE-style MCQs for Relational Model with full explanations. Attempt each question before revealing the answer.

Q1

R(A, B, C, D) has A as its only candidate key. How many super keys does R have?

  • A) 4
  • B) 7
  • C) 8
  • D) 15
Show answer & explanation

Answer: C

Any subset that contains A is a super key: 2^(4-1) = 8 subsets.

Q2 GATE-style super-key counting

R(A, B, C, D) has candidate keys {A} and {B, C}. The number of super keys is:

  • A) 8
  • B) 10
  • C) 12
  • D) 14
Show answer & explanation

Answer: B

8 (contain A) + 4 (contain BC) - 2 (contain A and BC) = 10.

Q3

Which one of the following statements about foreign keys is FALSE?

  • A) A foreign key can be NULL
  • B) A foreign key can refer to the same relation
  • C) A foreign key must always be part of the primary key of its own relation
  • D) Several tuples can have the same foreign-key value
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.

Q4

Emp(eid, did) with a foreign key did referencing Dept(did) and NO ACTION on delete. Which operation is rejected?

  • A) inserting a new Dept tuple
  • B) deleting an Emp tuple
  • C) deleting a Dept tuple that is referenced by some Emp tuple
  • D) inserting an Emp tuple with did = NULL
Show answer & explanation

Answer: C

Deleting a referenced parent would leave dangling foreign keys. The other operations are valid.

Q5

R(A, B) with |dom(A)| = 3 and |dom(B)| = 4. The maximum number of tuples in an instance of R is:

  • A) 7
  • B) 12
  • C) 81
  • D) 4096
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.)

Q6

R has 4 attributes and 5 tuples; S has 3 attributes and 6 tuples. The Cartesian product R x S has:

  • A) degree 7 and 30 tuples
  • B) degree 7 and 11 tuples
  • C) degree 12 and 30 tuples
  • D) degree 3 and 30 tuples
Show answer & explanation

Answer: A

Degrees add (4 + 3 = 7) and tuple counts multiply (5 * 6 = 30).

Q7

Entity integrity requires that:

  • A) foreign keys are not NULL
  • B) no attribute of the primary key is NULL
  • C) every relation has a foreign key
  • D) all attributes are atomic
Show answer & explanation

Answer: B

Entity integrity: primary-key attributes can never be NULL, so every tuple is identifiable.

Q8

Which statement about views is correct?

  • A) A view always stores its own copy of the data
  • B) Every view can be updated
  • C) A view is a virtual relation defined by a query and can hide columns for security
  • D) A view cannot reference another view
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.

Topic drill · 8 questions

8 practice questions — Relational Model

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

R(A, B, C) has candidate keys {A} and {B}. The number of super keys is:

  • A) 4
  • B) 5
  • C) 6
  • D) 7
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).

P2

The maximum number of candidate keys possible for a relation with 4 attributes is:

  • A) 4
  • B) 6
  • C) 8
  • D) 16
Show answer & explanation

Answer: B) 6

Candidate keys form an antichain, so the maximum is C(4, 2) = 6 (all two-attribute subsets).

P3

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:

  • A) 0
  • B) 1
  • C) 4
  • D) the deletion is rejected
Show answer & explanation

Answer: C) 4

SET NULL sets the foreign key of all four referencing tuples to NULL; no tuple is deleted.

P4

Which one of the following can never violate referential integrity?

  • A) deleting from the referenced relation
  • B) updating a referencing foreign-key value
  • C) inserting into the referenced relation
  • D) inserting into the referencing relation
Show answer & explanation

Answer: C) inserting into the referenced relation

A new parent row cannot make an existing child reference invalid.

P5

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:

  • A) 12
  • B) 144
  • C) 2^12
  • D) 2^7
Show answer & explanation

Answer: C) 2^12

There are 2 * 3 * 2 = 12 possible tuples, and every subset is an instance: 2^12.

P6

Logical data independence refers to the ability to:

  • A) change the physical file organization without changing the schema
  • B) change the conceptual schema without changing external views or application programs
  • C) change the hardware without changing the DBMS
  • D) change the operating system
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.

P7

In R(A, B, C, D) with the single candidate key {A, B}, the number of super keys is:

  • A) 2
  • B) 4
  • C) 8
  • D) 12
Show answer & explanation

Answer: B) 4

Super keys must contain AB: 2^(4-2) = 4 (AB, ABC, ABD, ABCD).

P8

Which of the following is NOT true for a relation in the formal relational model?

  • A) tuples are unordered
  • B) duplicate tuples are not allowed
  • C) attribute values are atomic
  • D) the order of tuples defines the primary key
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.

Quick revision · 8 cards

8 flashcards — Relational Model

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Super key vs candidate key vs primary key?
Q2 Super keys with one candidate key of size k in n attributes?
Q3 What is referential integrity?
Q4 Which operations can violate referential integrity?
Q5 Referential actions on delete?
Q6 Degree and cardinality of R x S?
Q7 Is a view a stored copy of data?
Q8 Relation vs SQL table?