Relational Model
In the relational model a table with k columns is a triple (C, D, R) of column names, column domains and a multiset of rows, and a database is a collection of such tables linked by primary and foreign keys.
Properties#
Terminology#
| Term | Meaning |
|---|---|
| Relation | Synonym for table |
| Tuple / row | A single element |
| Attribute / column | A position with name and domain |
| Degree | Number of columns |
| Cardinality | Number of rows |
| Schema | The pair describing the structure |
| Instance | The current multiset of rows |
Multiset, not set#
A set contains each element at most once, a multiset may contain it several times: has . Codd’s original relational model (1969) allowed only sets; most modern systems (and the course definition) allow multisets, because checking for duplicates costs time and space. If uniqueness is needed, it is declared as a constraint (UNIQUE, PRIMARY KEY).
No order#
Neither sets nor multisets have an order: a database need not remember insertion order and may reorder rows to work more efficiently. An order of the final result is requested with ORDER BY (see Ordering); a persistent order needs an explicit counter column (e.g. an auto-increment column, which costs time and space).
Primary key#
A set of key columns is a primary key if no two distinct rows agree on all of them:
Usually a single ID column (e.g. movies.id); it gives each row a unique ID and is the canonical way to reference a row from another table.
Foreign key#
A primary key used in another table is a foreign key there. A foreign key in referencing the primary key of requires referential integrity:
With PRIMARY KEY and REFERENCES declared, the RDBMS checks that every row has a primary key, that it is unique, and that every foreign-key value exists in the referenced table.
Relationships between tables#
Two tables , linked by keys (directly or via other tables) stand in one of four relationships:
| Kind | Meaning |
|---|---|
| 1:1 | each record in is associated with exactly one record in |
| 1:N | a record in can be associated with several records in , but each record in with only one in |
| N:1 | the same, the other way round |
| N:M | records in can be associated with several in and vice versa |
A link via a primary key and a foreign key is typically 1:N (never N:M); a link via a table of two foreign keys is typically N:M.
Closure#
The operations on tables (Relational Algebra: projection, selection, Cartesian Product, Join, Grouping) take tables and return a table, so they can be composed into query plans. SQL references columns by name ; the formal operations reference them by index .
Example#
Sources#
- Slides 03, p. 8 (table definition), p. 9–10 (multisets, order), p. 22–27 (IDs, keys, relationships)
- Slides 04, p. 7–8 (recap, concatenation and column access)
- DBIS_VL03 > Tables, DBIS_VL03 > Sets vs. multisets, DBIS_VL03 > Primary key and foreign key, DBIS_VL03 > Relationships between tables, DBIS_VL04 > Notation