R(A, B, C, D) has exactly two candidate keys, {A} and {B}. How many superkeys does R have? List them.
Keys identify tuples and link tables: superkeys, minimal candidate keys, the chosen primary key, alternate keys and foreign keys. Integrity constraints — domain, entity, referential, key, CHECK, NOT NULL — keep data valid, and superkey-counting numericals are PSC favourites.
A relation is a set of tuples, so no two tuples may be identical. That simple rule guarantees every relation has at least one set of attributes that tells its tuples apart — at worst, all of its attributes together. Keys are the attribute sets that do this job, and choosing them well is the first step of every design.
Note the words every legal instance. A key is a rule about all data the table may ever hold, decided by the meaning of the attributes — not by the rows that happen to be there today. Today's data can disprove a key (two rows share the value) but can never prove one.
Take student(roll_no, aadhaar, name, phone, dept). If roll numbers and Aadhaar numbers are unique, then {roll_no}, {aadhaar}, {roll_no, name}, {aadhaar, dept} and {roll_no, aadhaar, name, phone, dept} are all superkeys: any set containing roll_no or aadhaar identifies a student. {name, dept} is not, because two students named Asha may both study CS.
A candidate key is a superkey with nothing to spare: remove any attribute and it stops being a superkey. In the student example, {roll_no} and {aadhaar} are candidate keys, while {roll_no, name} is a superkey but not a candidate key because roll_no alone already works.
The designer picks one candidate key as the primary key; the others become alternate (secondary) keys and are declared UNIQUE. Good primary keys are never NULL, stable over time, short and simple — which is why an internal roll_no or emp_id usually beats a long, changeable natural value such as a phone number.
| Key type | Definition | Example in the sample database |
|---|---|---|
| Superkey | Any attribute set that uniquely identifies tuples | {emp_id}, {emp_id, ename}, all seven columns of emp |
| Candidate key | Minimal superkey | dept has two: {dept_id} and {dname} (dname is UNIQUE NOT NULL) |
| Primary key | The candidate key chosen; NOT NULL + unique | dept_id in dept, emp_id in emp |
| Alternate key | A candidate key not chosen as primary | dname in dept |
| Composite key | A key made of two or more attributes | {emp_id, proj_id} in works_on |
| Foreign key | References a key of another or the same table | emp.dept_id → dept; emp.manager_id → emp |
| Surrogate key | System-generated, no business meaning | An AUTO_INCREMENT or IDENTITY id column |
| Natural key | Identifier that exists in the real world | PAN, Aadhaar number, ISBN, vehicle registration number |
An attribute that belongs to at least one candidate key is a prime attribute; all other attributes are non-prime. In works_on(emp_id, proj_id, hours), emp_id and proj_id are prime and hours is non-prime. These two words drive the definitions of 2NF and 3NF later in this module, so always find all candidate keys first.
A natural key is an identifier that already exists in the real world — PAN, Aadhaar number, ISBN, vehicle registration number. A surrogate key is a meaningless number the system generates only to identify rows. Most production schemas use a surrogate primary key and keep the natural key as an alternate key with a UNIQUE constraint, getting the advantages of both.
| DBMS | Auto-generated surrogate key syntax |
|---|---|
| SQLite | id INTEGER PRIMARY KEY (aliases the rowid; add AUTOINCREMENT to forbid reuse of old ids) |
| MySQL | id INT AUTO_INCREMENT PRIMARY KEY |
| SQL Server | id INT IDENTITY(1,1) PRIMARY KEY |
| Oracle 12c+, PostgreSQL, SQL standard | id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY |
| Older Oracle | CREATE SEQUENCE emp_seq; then emp_seq.NEXTVAL in the INSERT (often via a trigger) |
CREATE TABLE vehicle (
vehicle_id INTEGER PRIMARY KEY, -- surrogate primary key
reg_no VARCHAR(12) NOT NULL UNIQUE, -- natural key kept as alternate key
chassis_no VARCHAR(17) NOT NULL UNIQUE, -- a second alternate key
owner_id INTEGER NOT NULL
REFERENCES allottee(allottee_id), -- foreign key
model VARCHAR(30)
);
CREATE TABLE vehicle_service (
vehicle_id INTEGER REFERENCES vehicle(vehicle_id),
service_no INTEGER,
service_on DATE NOT NULL,
PRIMARY KEY (vehicle_id, service_no) -- composite primary key
);Here vehicle has three candidate keys — vehicle_id, reg_no and chassis_no — of which vehicle_id is primary and the other two are alternate keys. In vehicle_service the composite key (vehicle_id, service_no) makes vehicle_id and service_no prime attributes and service_on non-prime.
A foreign key is a set of attributes in a referencing (child) relation whose values must match the primary key — or another UNIQUE key — of a referenced (parent) relation. In the sample database, emp.dept_id references dept.dept_id, project.dept_id references dept.dept_id, works_on.emp_id references emp, works_on.proj_id references project, and emp.manager_id references emp itself — a self-referencing (recursive) foreign key.
Referential integrity says every foreign-key value either matches an existing key value in the parent or is entirely NULL. Jose's dept_id is NULL (not yet posted to a department), which is allowed; a dept_id of 50 would be rejected because no department 50 exists. Foreign-key values may repeat — four employees share dept_id 20.
PRAGMA foreign_keys = ON; (it is off by default for backward compatibility). Run that line first in the SQL playground or the referential-integrity examples will silently succeed.Integrity constraints are rules every legal database state must satisfy. Declaring them in the schema lets the DBMS enforce them for every program, instead of trusting each program to check. The relational model and SQL provide the following kinds.
| Constraint | Rule | SQL syntax |
|---|---|---|
| Domain constraint | Each value comes from the attribute's domain — right type, format and range | Data type plus CHECK, e.g. salary DECIMAL(10,2) |
| NOT NULL | The attribute must always have a value | ename VARCHAR(30) NOT NULL |
| Key (UNIQUE) constraint | No two tuples share the key value | dname VARCHAR(20) UNIQUE |
| Entity integrity | No primary-key attribute may be NULL | PRIMARY KEY (emp_id) implies NOT NULL |
| Referential integrity | FK values match a parent key or are NULL | FOREIGN KEY (dept_id) REFERENCES dept(dept_id) |
| CHECK constraint | A row-level condition must not be false | CHECK (salary > 0), CHECK (due_date > issue_date) |
| Default | Value used when none is supplied (not strictly a constraint) | status CHAR(1) DEFAULT 'A' |
| Assertion / trigger | Rules spanning rows or tables | CREATE ASSERTION (standard, rarely implemented) or a trigger |
CREATE TABLE allottee (
allottee_id INTEGER PRIMARY KEY, -- entity integrity
aadhaar CHAR(12) NOT NULL UNIQUE, -- alternate key
name VARCHAR(40) NOT NULL,
district VARCHAR(20) DEFAULT 'Thrissur',
income INTEGER,
CONSTRAINT chk_income CHECK (income >= 0) -- named table-level CHECK
);A CHECK fails only when its condition is false. If income is NULL the condition income >= 0 is UNKNOWN, and the row is accepted — add NOT NULL when a value is compulsory. Naming constraints (CONSTRAINT chk_income …) makes error messages readable and lets you drop the constraint later.
When a parent row is deleted, or its key is updated, the DBMS must decide what happens to the children. The foreign-key declaration states the choice with ON DELETE and ON UPDATE clauses.
| Action | Effect on child rows | Typical use |
|---|---|---|
CASCADE | Delete the children too (ON DELETE) or copy the new key into them (ON UPDATE) | works_on rows of a deleted employee; order lines of a deleted order |
SET NULL | Set the children's foreign key to NULL | Employees of a closed department wait for re-posting |
SET DEFAULT | Set the foreign key to its column default | Move orphans to a default 'unassigned' department |
RESTRICT | Refuse the parent change immediately if children exist | Never delete a department that still controls projects |
NO ACTION (default) | Refuse if children still exist when the constraint is checked (end of statement; can be deferred) | Same effect as RESTRICT in most everyday cases |
PRAGMA foreign_keys = ON;
CREATE TABLE dept (dept_id INTEGER PRIMARY KEY, dname TEXT NOT NULL UNIQUE);
CREATE TABLE emp (
emp_id INTEGER PRIMARY KEY,
ename TEXT NOT NULL,
dept_id INTEGER REFERENCES dept(dept_id)
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE project (
proj_id INTEGER PRIMARY KEY,
pname TEXT NOT NULL,
dept_id INTEGER REFERENCES dept(dept_id)
ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE works_on (
emp_id INTEGER REFERENCES emp(emp_id) ON DELETE CASCADE,
proj_id INTEGER REFERENCES project(proj_id) ON DELETE CASCADE,
hours INTEGER,
PRIMARY KEY (emp_id, proj_id)
);ON DELETE CASCADE and ON DELETE SET NULL (no ON UPDATE actions); MySQL InnoDB, PostgreSQL, SQL Server and SQLite support the full set (MySQL treats RESTRICT and NO ACTION identically). RESTRICT differs from NO ACTION only when constraint checking is deferred to the end of a transaction.A superkey is any superset of a candidate key. If R has n attributes and a single candidate key K of k attributes, a superkey must contain all of K and may contain any selection of the remaining n − k attributes — each one either in or out.
Example: R(A, B, C, D) with candidate keys {A} and {B}. Supersets of A: 2³ = 8. Supersets of B: 8. Supersets of both (they contain A and B): 2² = 4. Total = 8 + 8 − 4 = 12.
A shortcut for single-attribute keys uses the complement: count the subsets that contain none of the keys and subtract from all non-empty subsets. With keys A, B, C in R(A, B, C, D, E, F), the subsets avoiding A, B and C are the 2³ subsets of {D, E, F} (including the empty set), so superkeys = 2⁶ − 2³ = 56.
| Relation and candidate keys | Working | Superkeys |
|---|---|---|
| R(A, B, C, D); key {A} | 2⁴⁻¹ | 8 |
| R(A, B, C); keys {A}, {B}, {C} | every non-empty subset: 2³ − 1 | 7 |
| R(A, B, C, D, E); keys {A}, {B, C}, {D} | 16 + 8 + 16 − 4 − 8 − 4 + 2 | 26 |
| R(A, B, C, D, E, F); keys {A, B}, {C, D}, {E, F} | 3·16 − 3·4 + 1 | 37 |
In exams, keys are usually derived from functional dependencies (FDs) rather than from an English description. A set X is a superkey exactly when its closure X⁺ — every attribute X determines — contains all attributes of R. The next topic develops closures fully; here is the idea in one example.
Try the problem first, then reveal the working one step at a time.
R(A, B, C, D) has exactly two candidate keys, {A} and {B}. How many superkeys does R have? List them.
(a) R(A, B, C, D, E) has candidate keys {A, B} and {C, D}. (b) R(A, B, C, D, E, F) has candidate keys {A} and {B, C}. Count the superkeys in each.
Using the schema with referential actions shown in this topic (emp.dept_id ON DELETE SET NULL ON UPDATE CASCADE; project.dept_id ON DELETE RESTRICT ON UPDATE CASCADE; works_on ON DELETE CASCADE) and the sample data, predict: (1) DELETE FROM dept WHERE dept_id = 40; (2) DELETE FROM dept WHERE dept_id = 30; (3) UPDATE dept SET dept_id = 25 WHERE dept_id = 20; (4) DELETE FROM emp WHERE emp_id = 102.
R(A, B, C, D, E, F) has three candidate keys {A}, {B} and {C}. How many superkeys does it have?
R(A, B, C, D) has candidate keys {A, B} and {B, C}. Count and list its superkeys.
This instance of student(roll, name, phone, dept) is all you have: (1, Asha, 9001, CS), (2, Binu, 9002, EC), (3, Asha, 9003, CS), (4, Dev, 9002, ME). Which of {name}, {phone}, {name, dept}, {roll}, {name, phone} does the instance rule out as keys? Can the instance prove that {roll} is a key?
Write a CREATE TABLE for issue(member_id, isbn, issue_date, due_date) in a library so that: the key is (member_id, isbn, issue_date); deleting a member deletes their issue records; a book with issue records cannot be deleted; due_date must be later than issue_date. Assume member(member_id) and book(isbn) exist.
On the sample database (default NO ACTION foreign keys, PRAGMA foreign_keys = ON) predict each result: (a) insert employee 111 with dept_id 50; (b) insert employee 112 with dept_id NULL; (c) DELETE FROM dept WHERE dept_id = 10; (d) UPDATE emp SET emp_id = 201 WHERE emp_id = 108; (e) insert an employee with salary −5.
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.