Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

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#

TermMeaning
RelationSynonym for table
Tuple / rowA single element r∈Rr \in R
Attribute / columnA position jj with name cjc_j and domain DjD_j
DegreeNumber of columns k=∥C∥k = \|C\|
CardinalityNumber of rows ∥R∥\|R\|
SchemaThe pair (C,D)(C, D) describing the structure
InstanceThe current multiset RR of rows

Multiset, not set#

A set contains each element at most once, a multiset may contain it several times: M={1,7,3,7,7}M = \{1, 7, 3, 7, 7\} has ∣M∣=5|M| = 5. 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 K⊆{1,…,k}K \subseteq \{1, \ldots, k\} is a primary key if no two distinct rows agree on all of them:

∀ r,r′∈R,  r≠r′  ⟹  ∃ j∈K:rj≠rj′.\forall\, r, r' \in R,\; r \neq r' \implies \exists\, j \in K : r_j \neq r'_j.

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 FF in T1T_1 referencing the primary key KK of T2T_2 requires referential integrity:

∀ r∈R1  ∃ r′∈R2:r[F]=r′[K].\forall\, r \in R_1 \;\exists\, r' \in R_2 : r[F] = r'[K].

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 AA, BB linked by keys (directly or via other tables) stand in one of four relationships:

KindMeaning
1:1each record in AA is associated with exactly one record in BB
1:Na record in AA can be associated with several records in BB, but each record in BB with only one in AA
N:1the same, the other way round
N:Mrecords in AA can be associated with several in BB 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 cjc_j; the formal operations reference them by index jj.

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only