03 / 19 · Models and Integrity
The relational model and algebra
Represent shop facts as relations and derive a receipt with selection, projection and join.
On this page 4
Order 106 has two lines for the same 50 g tea pack. One records a price of 350 cents and the other a price of 300. To print the receipt, the shop needs both line facts and the one catalog fact that supplies the pack’s code. A relational query combines these facts while keeping the two lines distinct.
A schema describes allowed tuples#
A relation schema names a collection of attributes and assigns a domain to each. A domain describes possible values, for example integer quantities or text catalog codes. A tuple assigns one value to each attribute. A relation instance is a set of tuples over the schema. The schema can stay fixed while its instance changes. There is no inherent order of tuples, and a set contains at most one copy of a tuple. (Codd, 1970, §1.3, Pavlo, 2025, §4)
Use the schema from the order example. Write for the order-line relation and for the catalog-item relation. In the algebra below, name order ID, line number, product ID, quantity and agreed unit price in cents. The catalog uses as well, and names the catalog code. These are attribute names, so refers to the product ID even when it appears at a different position in another relation.
| Relation | Attributes used here |
|---|---|
The two lines of order 106 are and . The catalog maps item 7 to TEA50 in the initial state. A tuple holds one line fact, even when its product is the same as a different line’s product.
Keys and references constrain instances#
A superkey is a set of attributes whose values distinguish tuples in every valid instance. A candidate key is a superkey that loses this guarantee when we remove any of its attributes. Here is a candidate key for . Order 106 has two lines, so alone repeats. Orders 101 and 106 both have a first line, so alone repeats. Including the quantity as well would give a superkey that is no longer minimal. In the catalog, is the chosen primary key, and is another candidate key under the rule that each current code is distinct and present. (Pavlo, 2025, §5, PostgreSQL 18, §§5.5.3–5.5.4)
A foreign key constrains references between instances. Every product ID appearing in must appear as a product ID in . In set notation this is , where keeps the listed attributes. It lets several lines reference the same catalog item and lets the catalog contain items without sales. In the SQL setup product_id is NOT NULL, so the foreign key requires a catalog match for every line. A nullable SQL foreign key can exempt a row from the usual match requirement. (PostgreSQL 18, §5.5.5)
Build a receipt from smaller operations#
Selection retains tuples satisfying a predicate. retains the two lines of order 106. Projection keeps only named attributes and removes repeated tuples because its result is a set. Join combines matching tuples. In this example the natural join matches on the only common attribute . We can express the receipt as a composition. (Codd, 1970, §§2.1.2–2.1.3, Pavlo, 2025, §6)
Each selected line has exactly one catalog match because the reference is required and is a catalog key. Projecting line number, code and unit price gives the two receipt tuples. The two line numbers keep the tuples distinct.
Two lines and one catalog item
To run the SQL below, create the order and catalog tables from the previous example before the optional code correction. The query names the shared join column explicitly.
SELECT l.line_no, c.catalog_code, l.unit_price_cents
FROM order_lines AS l
JOIN catalog_items AS c USING (product_id)
WHERE l.order_id = 106
ORDER BY l.line_no;
| line_no | catalog_code | unit_price_cents |
|---|---|---|
| 1 | TEA50 | 350 |
| 2 | TEA50 | 300 |
A Cartesian product would pair both selected lines with all three catalog items and give six pairs. The equality on retains two. In the algebra, selection, projection and join all return relations, so each result can become an input to another operation.
Where SQL differs from the model#
The projection contains the single tuple . A plain SELECT product_id over the two lines returns two rows with value 7. SQL preserves those occurrences by default. Add DISTINCT to request set-style projection.
SELECT DISTINCT product_id
FROM order_lines
WHERE order_id = 106;
These examples use relations with ordinary values, so a test such as is true or false. SQL adds the NULL marker and an unknown result for many comparisons. It also supports explicit result ordering. The algebra here reasons about tuple membership, while SQL examples must state their duplicate and missing-value behaviour. See types and NULL and duplicates and ordering. (PostgreSQL 18, SELECT)
Next#
A line number identifies a position only within an order. A product ID, by contrast, determines a catalog code wherever the product appears. Functional dependencies express both rules and show what happens when their facts are stored together.
References#
- Edgar F. Codd. A Relational Model of Data for Large Shared Data Banks. Communications of the ACM 13(6), 1970, §§1.3 and 2.1. Defines relations over domains, projection and join.
- Andy Pavlo. Relational Model and Algebra. Carnegie Mellon University, 15-445/645, Fall 2025, §§4–6. Develops the schema, keys and algebraic operators.
- PostgreSQL Global Development Group. PostgreSQL 18 documentation. Constraints, §§5.5.3–5.5.5, and SELECT. Defines SQL keys, null handling in references and duplicate behaviour.