Sub topic 2
Keys and integrity constraints
This chapter assumes you know what a relation, tuple and attribute are (the previous chapter) and basic set operations. It covers how keys identify tuples, how a foreign key ties two relations together, and which database changes violate which constraint. These ideas appear in GATE questions that count keys or ask which operation breaks referential integrity.
Superkeys, candidate keys and the primary key
A relation is a set of tuples, so no two tuples are identical. Keys make that guarantee precise.
- A superkey is a set of attributes whose values are unique across all tuples. No two tuples agree on every attribute of the set.
- A candidate key is a minimal superkey. Removing any attribute from it leaves a set that is no longer a superkey.
- The primary key is the one candidate key the designer chooses as the main identifier. The other candidate keys are called alternate keys.
The set of all attributes is always a superkey, because tuples in a set are distinct. Every superkey contains at least one candidate key.
Keys are properties of the schema, meaning what must hold in every legal instance. They are not properties of one table state. If a table happens to have no repeated values in a column, that does not make the column a key.
Counting superkeys
Suppose R(A, B, C, D) has candidate keys {A} and {B, C}. Count the superkeys with inclusion-exclusion.
- Supersets of {A}: the other 3 attributes are free, so 2^3 = 8.
- Supersets of {B, C}: 2^2 = 4.
- Supersets of both, which means supersets of {A, B, C}: 2^1 = 2.
Total: 8 + 4 − 2 = 10 superkeys.
A related fact: a relation on n attributes can have at most C(n, ⌊n/2⌋) candidate keys. No candidate key may contain another, so the keys form an antichain of subsets. For n = 4 the maximum is C(4, 2) = 6, reached when every 2-attribute subset is a key.
Integrity constraints
An integrity constraint is a rule that every legal database state must satisfy. The DBMS rejects any change that would break one.
| Constraint | Rule |
|---|---|
| Domain | Each value must belong to its attribute's domain (type, range). |
| Key | No two tuples share the same values on a key. |
| Entity integrity | No attribute of the primary key may be NULL. |
| Referential integrity | A foreign key value must be NULL or match an existing key value in the referenced relation. |
Foreign keys
A foreign key is an attribute set in one relation (the referencing, or child, relation) whose values refer to the primary key, or another candidate key, of another relation (the referenced, or parent, relation). The two sides must have compatible domains. The foreign key itself need not be unique in the child. Many enrollments can point to the same student.
A foreign key can be NULL unless it is declared NOT NULL or is part of the primary key. A tuple with a NULL foreign key passes the referential check, because it refers to nothing.
Which operations violate what
Take Student(sid, sname, age) as the parent and Enrolled(sid, cid) as the child with Enrolled.sid as a foreign key to Student.sid.
| Operation | Relation | Can violate |
|---|---|---|
| Insert | Parent | Key and entity integrity (duplicate or NULL key). Never referential integrity. |
| Insert | Child | Referential integrity (no matching parent), plus key constraints. |
| Delete | Parent | Referential integrity, if children still point to the tuple. |
| Delete | Child | Nothing. |
| Update of key | Parent | Referential integrity (orphans the children) and key constraints. |
| Update of foreign key | Child | Referential integrity. |
GATE questions often ask which of these is safe. Deleting from the child and inserting into the parent are the two operations that cannot break a foreign key.
Referential actions
When a parent delete or update would orphan children, the foreign key declaration decides the response.
NO ACTIONorRESTRICT: reject the operation. This is the default.CASCADE: apply the same delete or update to the matching child tuples.SET NULL: set the child foreign key to NULL. This fails if the column is NOT NULL.SET DEFAULT: set the child foreign key to its default value, which must itself be a valid parent key.
A worked example
The script below uses the PostgreSQL dialect and runs as written. It shows each kind of violation in turn.
CREATE TABLE Student (
sid INT PRIMARY KEY,
sname VARCHAR(50),
age INT
);
CREATE TABLE Course (
cid INT PRIMARY KEY,
cname VARCHAR(50)
);
CREATE TABLE Enrolled (
sid INT,
cid INT,
PRIMARY KEY (sid, cid),
FOREIGN KEY (sid) REFERENCES Student(sid) ON DELETE CASCADE,
FOREIGN KEY (cid) REFERENCES Course(cid)
);
INSERT INTO Student VALUES (1, 'Asha', 21);
INSERT INTO Course VALUES (10, 'DBMS');
INSERT INTO Enrolled VALUES (1, 10); -- ok
INSERT INTO Student VALUES (1, 'Ravi', 19); -- fails: duplicate primary key
INSERT INTO Student VALUES (NULL, 'Mei', 20); -- fails: entity integrity
INSERT INTO Enrolled VALUES (2, 10); -- fails: no Student with sid 2
DELETE FROM Course WHERE cid = 10; -- fails: Enrolled still references it
DELETE FROM Student WHERE sid = 1; -- ok: the Enrolled row is deleted tooThe Student delete succeeds because of ON DELETE CASCADE. The Course delete fails because that foreign key has the default NO ACTION.
Common errors
- Reading a key off one instance. If a table shows no repeated values in a column, that does not make the column a key. A key must hold in every legal instance.
- Calling a superkey a candidate key. {sid, sname} is a superkey of Student but not a candidate key, because {sid} alone suffices.
- Forgetting that foreign keys can be NULL. A NULL value never causes a referential violation.
- Assuming a UNIQUE column is a primary key. UNIQUE columns are alternate keys. In standard SQL they may hold several NULLs, since two NULLs are not considered equal. Some systems differ, for example SQL Server allows only one NULL.
- Assuming a foreign key must reference a primary key. In SQL it may reference any UNIQUE column set.
Exercise
R(A, B, C) has candidate keys {A} and {B}. How many superkeys does R have?
Show answer
Supersets of {A}: 2^2 = 4. Supersets of {B}: 4. Supersets of both, which are the supersets of {A, B}: 2. Total 4 + 4 − 2 = 6 superkeys.
Exercise
Enrolled(sid, cid) has a foreign key sid referencing Student(sid) with ON DELETE SET NULL, and sid is part of Enrolled's primary key (sid, cid). What happens when a student with enrollments is deleted?
Show answer
The delete is rejected. SET NULL would put NULL into sid, which is part of the primary key, and entity integrity forbids that. Cascade or restrict would be the workable choices here.
Exercise
Which single operation can never violate referential integrity: inserting into the parent, deleting from the parent, inserting into the child, or updating a child's foreign key?
Show answer
Inserting into the parent. A new parent tuple orphans nothing and removes no referenced value. It can still violate key or entity integrity.