Sub topic 2
The relational model: relations, tuples, and attributes
This chapter assumes you can read short code snippets and know what a spreadsheet table is. It also assumes you know that applications store data somewhere. It introduces the vocabulary of the relational model. The later chapters on tables, keys, and SQL build on that vocabulary.
Why a formal model exists
In 1970, Edgar F. Codd proposed describing data with a small mathematical idea: the relation. Before that, programs often had to know how data was physically laid out in files or linked records. If the layout changed, the programs broke.
Codd's model separates what the data means from how it is stored. You describe the data as relations and ask questions about it. The database software decides how to find the answer. This separation is called data independence. It is why you can add an index or move storage around without rewriting your queries.
The three core terms
The model has three terms. Each has an everyday counterpart in SQL.
| Relational model term | Everyday SQL term | Meaning |
|---|---|---|
| Relation | Table | A named collection of facts that all have the same shape |
| Tuple | Row | One fact: a single combination of values |
| Attribute | Column | A named property that every tuple has |
Two further terms complete the picture:
- A domain is the set of values an attribute may hold. For example, the domain of
emailis text of up to 255 characters. SQL approximates domains with data types and constraints. - A relation schema is the relation's name plus its attributes and their domains. A relation instance is the set of tuples stored at a given moment. The schema changes rarely. The instance changes constantly.
Take a relation named customers with attributes customer_id, name, and email. The schema is customers(customer_id, name, email). The instance is whatever rows are in it right now.
What "relation" really means
The word "relation" does not mean a relationship between two tables. A relation is a mathematical object. Given a list of domains, a relation is a set of tuples whose values come from those domains.
Each tuple states one fact. The tuple (1, 'Ada Lovelace', '[email protected]') says: there is a customer with ID 1, named Ada Lovelace, with this email address. The relation is the collection of all such facts of one kind.
This is why the model uses the word "relation." A tuple relates its values to one another. The values 1, "Ada Lovelace", and "[email protected]" are related because they describe the same customer. Connections between different tables come later, through keys.
Properties that follow from the definition
Because a relation is a set of tuples, several properties hold in the pure model. Each one affects how you design and query real tables.
Tuples have no order
A set has no first or last element. A relation does not store its rows in any particular order. Rows 1 and 2 could be swapped and the relation would be the same.
In SQL, this means a SELECT without ORDER BY gives you no guaranteed order. Many databases return rows in insertion order on small tables, which hides the problem until the data grows or an index changes the plan. If order matters, say so with ORDER BY.
Attributes are identified by name
In a spreadsheet you refer to "column C." In the relational model you refer to the attribute email. The order of columns carries no meaning. This is why you should write INSERT INTO customers (customer_id, name, email) ... with the column list instead of relying on the table's column order.
Each value is atomic
In the classic model, every attribute value in a tuple is a single value from its domain, not a list or a nested table. You do not store 'red,green,blue' in one column and expect the database to treat it as three colors. If a fact has several values, you model it as several tuples, usually in another relation. The chapter on normalization covers this rule in detail. Some modern databases offer array and JSON types, and those relax this rule on purpose.
No duplicate tuples
A set cannot contain the same element twice, so a pure relation never holds two identical tuples. This is the property where SQL differs most from the model.
Where SQL departs from the model
SQL is the language most relational databases use, but it follows the model loosely. SQL tables are closer to bags, which are collections that may contain duplicates. Run this example to see the difference. It works in PostgreSQL, MySQL, and SQLite.
CREATE TABLE visits (
visitor VARCHAR(50) NOT NULL,
page VARCHAR(50) NOT NULL
);
INSERT INTO visits (visitor, page) VALUES ('ada', '/home');
INSERT INTO visits (visitor, page) VALUES ('ada', '/home');
INSERT INTO visits (visitor, page) VALUES ('grace', '/home');
SELECT page FROM visits;
SELECT DISTINCT page FROM visits;The table accepts two identical rows, because nothing forbids it. The first SELECT returns /home three times. The SELECT DISTINCT returns it once, which is what a set-based relation would give you.
The fix at the design level is a key. A primary key (covered in a later chapter) guarantees that no two rows are the same. A table with a primary key behaves like a true relation in this respect.
Other differences you will meet:
- SQL allows
NULL, a marker for a missing or unknown value. The pure model has no such marker, andNULLforces SQL to use three-valued logic (true, false, unknown). The data types andNULLchapter covers it. - SQL columns have a left-to-right order. You can still ignore it by naming columns explicitly.
- SQL result sets can have duplicates and, with
ORDER BY, an order. A query result is not strictly a relation.
A concrete relation
Here is the relation from earlier written as a table definition and populated with two tuples.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
INSERT INTO customers (customer_id, name, email) VALUES
(1, 'Ada Lovelace', '[email protected]'),
(2, 'Grace Hopper', '[email protected]');Reading this in model terms:
- The relation is
customers. - Its attributes are
customer_id,name, andemail. - The domain of
customer_idis integers. The domain ofnameis text up to 100 characters. - Its degree, the number of attributes, is 3.
- Its cardinality, the number of tuples, is 2.
Each insert adds one tuple. The tuple (2, 'Grace Hopper', '[email protected]') is one fact. Changing it later changes the instance but not the schema.
Operations on relations: relational algebra
Because relations are sets, you can define operations on them that take relations in and give relations out. This set of operations is called relational algebra. Every result is itself a relation, so you can chain operations. That closure property is what makes queries composable.
The three operations you will use most map directly onto SQL clauses.
| Operation | What it does | SQL form |
|---|---|---|
| Selection | Keeps the tuples that satisfy a condition | `WHERE` |
| Projection | Keeps only the named attributes | The column list in `SELECT` |
| Join | Combines tuples from two relations that match on some attributes | `JOIN ... ON` |
Here selection and projection appear together:
SELECT name, email
FROM customers
WHERE customer_id = 1;WHERE customer_id = 1 is the selection: it filters to one tuple. SELECT name, email is the projection: it drops customer_id. The result is a new relation with two attributes and one tuple.
The set operations union, intersection, and difference also exist. They require the two relations to have matching attributes. SQL exposes them as UNION, INTERSECT, and EXCEPT (spelled MINUS in Oracle). Joins get their own chapter.
You do not need to write relational algebra to use a database. It matters because query optimizers use it internally. The database rewrites your query into algebra, applies equivalence rules such as filtering early, and picks an efficient plan. That is possible only because the model has precise semantics.
Common errors and misconceptions
- Treating "relation" as "relationship." A relation is a table-like set of facts. A relationship between tables is something you build with keys.
- Relying on row order. Without
ORDER BY, order is unspecified. Tests that pass by accident break later. - Assuming no duplicates in SQL. Without a key or
DISTINCT, duplicates appear. They also makeCOUNT(*)and joins return surprising totals. - Storing several values in one column. A comma-separated list breaks atomicity. You lose the ability to filter, join, or constrain the individual values.
- Confusing schema and instance. Adding a row changes the instance. Adding a column changes the schema. They have very different costs in a large table.
Summary
A relation is a set of tuples. Each tuple has the same attributes, and each attribute draws its values from a domain. SQL tables, rows, and columns implement these ideas with some differences: duplicates, NULL, and column order. Relational algebra gives queries a precise meaning, which is why the database can optimize them.
Exercise
A table visits(visitor, page) contains the rows ('ada', '/home'), ('ada', '/home'), and ('grace', '/about'). (a) How many tuples would the equivalent relation in the pure model contain? (b) What does SELECT DISTINCT page FROM visits return? (c) Which SQL clause corresponds to selection, and which to projection?
Show answer
(a) Two. A relation is a set, so the duplicate ('ada', '/home') collapses into one tuple. (b) Two rows: '/home' and '/about', in no guaranteed order. (c) WHERE is selection, because it filters tuples. The column list after SELECT is projection, because it chooses attributes.
Exercise
The table customers(customer_id, name, email) has 500 rows. What are its degree and its cardinality? Does adding a phone column change the degree or the cardinality?
Show answer
The degree is 3 and the cardinality is 500. Adding a phone column raises the degree to 4 and leaves the cardinality at 500, because it changes the schema, not the number of tuples.