Course contents 03 / 19

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 LL for the order-line relation and PP for the catalog-item relation. In the algebra below, o,n,p,q,co,n,p,q,c name order ID, line number, product ID, quantity and agreed unit price in cents. The catalog uses pp as well, and kk names the catalog code. These are attribute names, so pp refers to the product ID even when it appears at a different position in another relation.

RelationAttributes used here
LL(o,n,p,q,c)(o,n,p,q,c)
PP(p,k)(p,k)

The two lines of order 106 are (106,1,7,1,350)(106,1,7,1,350) and (106,2,7,1,300)(106,2,7,1,300). 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 (o,n)(o,n) is a candidate key for LL. Order 106 has two lines, so oo alone repeats. Orders 101 and 106 both have a first line, so nn alone repeats. Including the quantity as well would give a superkey that is no longer minimal. In the catalog, pp is the chosen primary key, and kk 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 LL must appear as a product ID in PP. In set notation this is πp(L)⊆πp(P)\pi_p(L)\subseteq\pi_p(P), where π\pi 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 σ\sigma retains tuples satisfying a predicate. σo=106(L)\sigma_{o=106}(L) retains the two lines of order 106. Projection π\pi keeps only named attributes and removes repeated tuples because its result is a set. Join ⋈\bowtie combines matching tuples. In this example the natural join matches on the only common attribute pp. We can express the receipt as a composition. (Codd, 1970, §§2.1.2–2.1.3, Pavlo, 2025, §6)

πn,k,c(σo=106(L)⋈P).\pi_{n,k,c}\bigl(\sigma_{o=106}(L)\bowtie P\bigr).

Each selected line has exactly one catalog match because the reference is required and pp is a catalog key. Projecting line number, code and unit price gives the two receipt tuples. The two line numbers keep the tuples distinct.

Figure

Two lines and one catalog item

Two lines and one catalog itemThe catalog attaches code TEA50 to line 1 at 350 cents and line 2 at 300 cents.P: (7, TEA50)L: 106/1 · 7 · 3501 · TEA50 · 350L: 106/2 · 7 · 3002 · TEA50 · 300
Item 7 matches both lines of order 106. The projection keeps both prices and line numbers.

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_nocatalog_codeunit_price_cents
1TEA50350
2TEA50300

A Cartesian product would pair both selected lines with all three catalog items and give six pairs. The equality on pp 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 πp(σo=106(L))\pi_p(\sigma_{o=106}(L)) contains the single tuple (7)(7). 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 o=106o=106 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#

  1. 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.
  2. Andy Pavlo. Relational Model and Algebra. Carnegie Mellon University, 15-445/645, Fall 2025, §§4–6. Develops the schema, keys and algebraic operators.
  3. 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.