Relational foundations and ER modelling
Start with what a row represents and what uniquely identifies it. ER constraints describe the real-world associations; keys and foreign keys express part of those constraints in tables.
1.1 Relations, schemas and database levels
Past papers · CS 2014–2026 (23 papers): 2 questions, 2 marks
A relation schema names an attribute set and its domains. An instance is a finite set of tuples over those domains. Degree or arity is the number of attributes; cardinality is the number of tuples. Attribute order and tuple order have no logical significance. SQL tables may contain duplicate rows; a mathematical relation cannot.
| Level | Describes | Independence |
|---|---|---|
| External | A user's view of selected data | Different users can see different views |
| Conceptual or logical | Relations, attributes and constraints | Logical changes can be hidden from external views |
| Internal or physical | Files, blocks and indexes | Physical changes can be hidden from the logical schema |
A view is a named query; an ordinary view need not store its result. A materialised view does store results and must be maintained when base data changes. Changing an index is a physical change; adding a relation is a logical change.
1.2 Keys and superkey counting
Past papers · CS 2014–2026 (23 papers): 6 questions, 7 marks
A superkey uniquely determines every attribute in all legal instances. A candidate key is an inclusion-minimal superkey: removing any one of its attributes destroys that property. Minimal does not mean smallest cardinality among all keys. The primary key is one chosen candidate key; the remaining candidate keys are alternate keys. An attribute is prime if it belongs to at least one candidate key.
An instance with no duplicate values does not prove a schema-level key. To disprove a proposed key in an instance, find two distinct rows agreeing on its columns.
1.3 Integrity and referential actions
Past papers · CS 2014–2026 (23 papers): 3 questions, 3 marks · DA 2024–2026 (3 papers): 2 questions, 3 marks
Domain constraints restrict allowed values. Key constraints restrict duplication. Entity integrity disallows NULL in every component of the primary key. Referential integrity requires a non-null foreign-key value to match a referenced candidate/unique key. A foreign key need not be unique and may refer to the same table. A table can have several foreign keys.
For a composite reference, compare the entire tuple of referencing columns, in corresponding order. One component of a composite primary key is not automatically a legal target on its own. Under the usual SQL MATCH SIMPLE, any null component exempts that row from the matching requirement; NOT NULL rules still apply. MATCH FULL permits either all-null components or a completely non-null matching tuple.
| Operation | Possible foreign-key problem |
|---|---|
| Insert child; update child's foreign key | New reference may have no parent |
| Delete parent; update referenced key | Existing children may lose their parent |
| Insert parent; delete child | Cannot by itself create an orphan for this particular reference |
| Parent action | Effect on referencing rows |
|---|---|
RESTRICT | Reject the change when references block it |
NO ACTION | Check the constraint at its configured checking time; not a synonym for ignoring it |
CASCADE | Delete children on parent deletion; propagate the changed key on update |
SET NULL | Keep children and null the referencing columns, if allowed |
SET DEFAULT | Use defaults; the resulting reference must still be valid |
1.4 ER symbols, cardinality and participation
Past papers · CS 2014–2026 (23 papers): 4 questions, 4 marks
An entity is distinguishable in the model; a relationship links entities in named roles. A recursive relationship uses the same entity type more than once, so role names matter. A relationship can have attributes of its own. A ternary relationship is generally not equivalent to three binary relationships: the allowed triples may be lost.
| Chen notation | Meaning |
|---|---|
| Rectangle / diamond | Entity set / relationship set |
| Oval; underlined attribute | Attribute; key attribute |
| Oval with component ovals | Composite attribute |
| Double oval / dashed oval | Multivalued / derived attribute |
| Double rectangle / double diamond | Weak entity / identifying relationship |
| Double participation line | Every entity on that side participates |
For as many-to-one, each entity has at most one partner in . Total participation of changes “at most one” to “exactly one”. Total participation of requires at least one incoming partner; it does not impose uniqueness.
A weak entity is identified by its owner's key together with its partial key, also called discriminator. Its participation in the identifying relationship is total; the owner's need not be. Partial keys are commonly drawn with dashed underlining.
1.5 Converting ER models to relations
Past papers · CS 2014–2026 (23 papers): 2 questions, 3 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
| ER construct | Relational representation |
|---|---|
| Strong entity | One relation with its key and simple attributes; flatten composite attributes |
| Weak entity | Owner key plus partial key form the primary key; include weak entity attributes |
| Multivalued attribute | Separate relation with owner key and the attribute value; their combination identifies values |
| Many-to-one | Put the one-side key as a foreign key in the many-side relation; place relationship attributes there |
| One-to-one | A foreign key with UNIQUE on a suitable side; prefer a total-participation side to avoid nulls |
| Many-to-many | Separate relation containing both entity keys and relationship attributes |
| Ternary or higher relationship | Separate relation; determine its key from the actual cardinality constraints |
For a many-to-one mapping, total participation of the many side permits a non-null foreign key and removal of a separate relationship table. Partial participation needs nullable references or a separate relationship relation. Total participation on both sides of a many-to-many relationship does not remove the need for its association table when the final design must remain in 3NF and no extra dependencies are given.
Specialisation inherits the superclass key and attributes. A superclass table plus one table per subclass is a general mapping: each subclass key is also a foreign key to the superclass. Disjoint/overlapping and total/partial constraints require separate enforcement; tables alone do not automatically enforce all of them.
Last-minute recall
- Degree counts columns; cardinality counts rows.
- A candidate key is minimal by inclusion; a superkey may contain extra attributes.
- Prime means present in at least one candidate key.
- A foreign key need not be a key in its own table.
- Cardinality gives the maximum; participation gives the minimum.
- A weak entity needs its owner key and partial key.
- Multivalued attributes and many-to-many associations normally need separate tables.