Sub topic 2
Keys and Integrity Constraints
Assume you know that a relation is a table-like set of tuples, or rows, and that each attribute has a defined domain of values. This chapter explains how databases identify tuples and prevent invalid relationships between tables.
Superkeys and candidate keys
A superkey is a set of one or more attributes that uniquely identifies every tuple in a relation. It may contain unnecessary attributes.
A candidate key is a minimal superkey. “Minimal” means that removing any attribute would destroy uniqueness.
Consider:
| sid | name | age | |
|---|---|---|---|
| 101 | [email protected] | Asha | 21 |
| 102 | [email protected] | Ben | 20 |
Both sid and email can identify each student. Therefore, both are candidate keys. The set {sid, email} is a superkey, but it is not a candidate key because either attribute alone is sufficient.
A relation can have several candidate keys. The database designer chooses one candidate key as the primary key. The remaining candidate keys are often enforced with UNIQUE constraints.
A primary key must be unique and cannot be NULL. A NULL value means that a value is missing or unknown. It cannot identify a tuple reliably.
Foreign keys and referential integrity
A foreign key is an attribute, or a set of attributes, in one relation that refers to a candidate key in another relation. The referenced relation is the parent, and the relation containing the foreign key is the child.
For example, Enrolled.sid can refer to Student.sid. Referential integrity requires every non-NULL foreign-key value in the child to match a key value in the parent. An enrollment for sid = 999 is invalid if no student 999 exists.
CREATE TABLE Student (
sid INTEGER PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
name TEXT NOT NULL
);
CREATE TABLE Course (
cid INTEGER PRIMARY KEY,
cname TEXT NOT NULL
);
CREATE TABLE Enrolled (
sid INTEGER,
cid INTEGER,
PRIMARY KEY (sid, cid),
FOREIGN KEY (sid) REFERENCES Student(sid)
ON DELETE CASCADE,
FOREIGN KEY (cid) REFERENCES Course(cid)
ON DELETE CASCADE
);The composite primary key (sid, cid) prevents the same student from being enrolled in the same course twice. It also makes each enrollment tuple identifiable by the pair, not by either attribute separately.
Constraint actions
A deletion or update of a parent key can affect child rows. Common referential actions include:
CASCADE: propagate the deletion or update to matching child rows.RESTRICTorNO ACTION: reject the parent operation when children exist.SET NULL: replace the foreign-key value withNULL, if the column permits it.
GATE checks
When a question asks whether a database state is valid, check primary-key uniqueness and non-nullness first. Then check every foreign-key value against the referenced relation. For a composite key, compare the complete attribute tuple.
Exercise
Student contains sid values 1 and 2. Enrolled contains (1, 10), (1, 10), and (3, 20). Which constraints are violated if (sid, cid) is the primary key and both attributes are foreign keys?
Show answer
The duplicate (1, 10) violates the composite primary key. The row with sid 3 violates the foreign key to Student. The cid value 20 is valid only if Course contains cid 20.