NIMCET, GATE, CUET & CBSE test series are live — start practicing free
syllabuzAI

Keys and Integrity Constraints in DBMS | GATE CSE

Learn super, candidate, primary, alternate and foreign keys for GATE CSE DBMS, how to count super keys, and how referential integrity and ON DELETE CASCADE work.

7 min read

AS

Aman Singh

Updated 26 November 2026

On this page

Quick answer: A candidate key is a minimal set of attributes that identifies every row of a table. One candidate key is chosen as the primary key (unique and never null), the others are alternate keys, and any superset of a candidate key is a super key. A foreign key must match an existing key value in the referenced table or be null. If a relation has n attributes and a single one-attribute candidate key, it has 2n−12^{n-1} super keys.

GATE often tests key counting and referential actions, because both are quick to ask and easy to get wrong. This topic is part of the ER Model and Relational Model chapter.

Basic relational terminology

TermMeaning
RelationA table
TupleA row (record)
AttributeA column (field)
Degree (arity)Number of attributes
CardinalityNumber of tuples
DomainSet of allowed values for an attribute

In the relational model a relation is a set of tuples, so no two rows can be identical. That is why every relation has at least one key.

Types of keys

KeyDefinition
Super keyAny set of attributes that identifies rows uniquely (a superset of some candidate key)
Candidate keyA minimal super key: no proper subset is a super key
Simple candidate keyA candidate key with one attribute
Composite (compound) keyA candidate key with two or more attributes
Primary keyThe candidate key chosen by the designer; unique and not null; at most one per table
Alternate keyAny candidate key that is not the primary key
Foreign keyAttributes that refer to the primary key (or another unique key) of the same or another table

Prime and non-prime attributes

  • Prime attribute: belongs to at least one candidate key.
  • Non-prime attribute: belongs to no candidate key.

Example: R(A, B, C, D, E) has candidate keys {A, B} and {B, C}. Both keys are composite and they overlap on B. Prime attributes are A, B and C. Non-prime attributes are D and E.

How to count super keys

A set is a super key if it contains at least one candidate key. For a relation with n attributes:

Candidate keysNumber of super keys
{A1}2n−12^{n-1}
{A1, A2} (one composite key)2n−22^{n-2}
{A1} and {A2}2n−1+2n−1−2n−22^{n-1} + 2^{n-1} - 2^{n-2}
{A1, A2} and {A3, A4}2n−2+2n−2−2n−42^{n-2} + 2^{n-2} - 2^{n-4}
Every single attribute is a candidate key2n−12^{n} - 1
Only the full set of n attributes is a key1

The rule behind every row is inclusion–exclusion: add the supersets of each candidate key, subtract the supersets of each pair's union, add back the triples, and so on. A superset of a key K in an n-attribute relation can freely include or leave out each of the other attributes, giving 2n−∣K∣2^{n-|K|} choices.

Worked example 1

R(A, B, C, D) with candidate key {A}.

Step 1: The other 3 attributes can each be in or out.

23=82^{3} = 8

So R has 8 super keys.

Worked example 2

R(A, B, C, D) with candidate keys {A} and {B, C}.

Step 1: Supersets of {A}:

23=82^{3} = 8

Step 2: Supersets of {B, C}:

22=42^{2} = 4

Step 3: Supersets of the union {A, B, C}, counted twice above:

21=22^{1} = 2

Step 4: Apply inclusion–exclusion:

8+4−2=108 + 4 - 2 = 10

So R has 10 super keys.

Entity integrity

The primary key cannot be null, because a null value cannot identify a row. In SQL, a PRIMARY KEY behaves like UNIQUE plus NOT NULL.

Referential integrity

The referenced (parent) table holds the key. The referencing (child) table holds the foreign key.

Example: Student(Sid, Sname, Age) is the parent. Enroll(Sid, Cid, Fee) is the child, and Enroll.Sid references Student.Sid.

Which operations can break referential integrity?

OperationOn the parent tableOn the child table
InsertNeverCan (a foreign key value with no matching parent)
DeleteCan (child rows lose their parent)Never
UpdateCan (if the key value changes)Can (if the foreign key changes)

An insert or update on the child that breaks the rule is rejected.

Referential actions when a parent row is deleted or updated

ActionEffect
NO ACTION / RESTRICTReject the delete or update if any child row refers to the parent
CASCADEDelete (or update) the matching child rows too
SET NULLSet the child's foreign key to null
SET DEFAULTSet the child's foreign key to its default value

Note: SET NULL needs a nullable foreign key. If the column is NOT NULL, the action cannot succeed and the delete is rejected.

Worked example: cascading delete in a self-referencing table

Table T(A, B): A is the primary key, B is a foreign key referencing A, with ON DELETE CASCADE.

AB
24
34
45
54
62

Step 1: Delete (2, 4).

Step 2: Row (6, 2) has B = 2, so it is deleted as well.

Step 3: No row has B = 6, so the cascade stops.

Remaining rows: (3, 4), (4, 5), (5, 4).

Domain and other constraints

  • Domain constraint: every value must come from the attribute's domain (type and range).
  • NOT NULL, UNIQUE and CHECK restrict values in individual columns.

Summary table

FactValue
Primary keys per tableAt most 1
Candidate keys per tableAt least 1
PRIMARY KEY in SQLUNIQUE + NOT NULL
Super keys, one single-attribute key2n−12^{n-1}
Super keys, every attribute a key2n−12^{n} - 1
Operations that can break referential integrityParent delete/update, child insert/update

Exam tips

  • To count super keys with two or more candidate keys, always use inclusion–exclusion on the union of keys, never on the keys themselves.
  • When tracing a cascade, keep a list of deleted key values and keep scanning until a full pass deletes nothing new.
  • Prime attributes come from all candidate keys, not just the primary key.

Common mistakes

  • Calling a non-minimal set a candidate key. Check that no proper subset is also a key.
  • Using 2n2^{n} instead of 2n−12^{n} - 1 when every attribute is a key. The empty set is not a key.
  • Thinking an insert into the parent can break referential integrity. It can't.
  • Stopping a cascade too early. Follow every chain of references.

Practice questions

Q1. R(A, B, C, D, E) has the single candidate key {A, B}. How many super keys does it have?

Answer: 25−2=82^{5-2} = 8.

Q2. R(A, B, C) has candidate keys {A}, {B} and {C}. How many super keys?

Answer: 23−1=72^{3} - 1 = 7.

Q3. In the cascade example above, which rows remain if (4, 5) is deleted instead?

Answer: Rows with B = 4, namely (2, 4), (3, 4) and (5, 4), are deleted. Then (6, 2) goes because 2 was deleted. The table ends up empty.

FAQs

What is the difference between a super key and a candidate key?

A super key is any set of attributes that identifies rows uniquely. A candidate key is a super key with no extra attribute, so removing any attribute from it breaks uniqueness.

Can a foreign key be null?

Yes, unless the column is declared NOT NULL. A null foreign key means the row does not refer to any parent row.

Can a foreign key refer to the same table?

Yes. A self-referencing foreign key, such as manager_id in an Employee table pointing to another employee, is common.

How many primary keys can a table have?

Only one. A table can have many candidate keys, but just one is chosen as the primary key.

How many super keys does a relation with n attributes have if every attribute is a candidate key?

It has 2ⁿ − 1 super keys, because every non-empty subset of attributes contains a key.

Conclusion

Practise the super key formulas and trace cascades step by step; both show up regularly in GATE. Next, see how ER diagrams turn into tables in ER diagram to relational tables, or go back to the ER Model and Relational Model chapter. Finding candidate keys from functional dependencies is covered in functional dependencies.

Concept Check

EASY

Which one of the following is NOT a part of the ACID properties of database transactions?

Suggested for you