n-ary Relations and Named Columns
From binary to n-ary
A binary relation involves two domains. Database tables rarely have only two columns. The generalisation is straightforward: if we have sets (domains) , then an n-ary relation is:
Each element of is an n-tuple, a sequence of values drawn from the respective domains.
Tabular presentation
N-ary relations are naturally displayed as tables:
| name | sid | age |
|---|---|---|
| Alice | abc123 | 20 |
| Bob | def456 | 21 |
| Carol | ghi789 | 20 |
Here . Each row is a tuple (Alice, abc123, 20). The three columns correspond to the three domains: string, string, integer.
The problem with positional columns
In the mathematical definition, columns are identified by position: the first column corresponds to , the second to , and so on. In a query language, writing “select column 3” is error-prone and unreadable.
Named columns and attribute names
Database relations use named columns: we associate a name (called an attribute name) with each domain .
Instead of tuples , we use records: sets of pairs each associating an attribute name with a value in domain . A record is:
where .
Column order does not matter
In a mathematical tuple, the order of components matters: . In a record with named attributes, the order of the pairs does not matter:
This matches how SQL works: SELECT name, age FROM Students and SELECT age, name FROM Students both produce valid results; the column order in the output is determined by the SELECT clause, not by any underlying ordering.
Schema notation
A database relation is therefore defined as a finite set:
The schema of is the set of attribute-name/domain pairs. Schema notation:
Example schema.
Row order
Just as column order does not matter, row order does not matter in the relational model. A relation is a set of records. SQL results are unordered unless you explicitly include ORDER BY.
In practice, rows are stored in some physical order on disk (often a B-tree on the primary key), but the relational model does not expose or rely on that ordering.
Summary
- An n-ary relation generalises binary relations to domains.
- Database relations use named columns (attribute names) rather than positional columns.
- Column order and row order do not matter at the logical level.
- A schema declares the attribute names and their types.