Sub topic 2
The Relational Model: Relations, Tuples, and Attributes
Assume you are comfortable reading tabular data and short code snippets, and you understand why an application needs to store information. This chapter explains the model behind relational databases. Later chapters can use this model to discuss keys, relationships, and queries.
From tables to relations
A relational database represents data using relations. A relation is a set of related values organized according to a defined structure. In practice, a relation usually appears as a table.
A relation has:
- Attributes, which are the named fields or columns.
- Tuples, which are the individual records or rows.
- A relation schema, which defines the attribute names and their data types.
- A relation instance, which is the actual set of tuples stored at a particular time.
For example, a customer relation might have this schema:
| Attribute | Meaning | Example type |
|---|---|---|
| customer_id | Identifier for a customer | integer |
| name | Customer's name | text |
| Customer's email address | text |
An instance of that relation could contain these tuples:
| customer_id | name | |
|---|---|---|
| 1 | Ada Lovelace | [email protected] |
| 2 | Grace Hopper | [email protected] |
In everyday database language, people usually say “table,” “column,” and “row.” The relational terms are useful because they describe the underlying model precisely.
What the model guarantees
A relation schema gives every value a place and a meaning. The email attribute should contain email values, not dates or arbitrary notes. Each tuple must follow the same attribute structure.
The relational model also treats attributes as named values rather than positions alone. A query can ask for the email attribute directly. It does not need to remember that email happens to be the third column.
In the formal model, a relation is a set, so duplicate tuples do not exist and row order has no meaning. SQL differs in two important ways:
- SQL tables commonly allow duplicate rows unless a constraint prevents them.
- SQL results have an order only when you use
ORDER BY.
Do not use the visual order of rows to represent meaning. If order matters, store an attribute such as position or created_at, then request that order explicitly.
CREATE TABLE customers (
customer_id INTEGER,
name VARCHAR(100),
email VARCHAR(255)
);
INSERT INTO customers (customer_id, name, email)
VALUES
(1, 'Ada Lovelace', '[email protected]'),
(2, 'Grace Hopper', '[email protected]');
SELECT customer_id, name, email
FROM customers
ORDER BY customer_id;Schema and instance
The schema is the plan. It describes which attributes exist and what kind of values they accept. The instance is the current data that follows that plan.
Changing a tuple changes the instance. Adding an attribute or changing its type changes the schema. This distinction matters because applications depend on the schema, while the stored rows change during normal operation.
A relation should describe one kind of thing or one kind of fact. A customer relation describes customers. An order relation describes orders. Keeping different facts in separate relations makes their meaning clear and allows the database to connect them through shared values.
A visual model
Common errors
- Treating a row's position as its identity.
- Assuming two columns with similar names automatically connect tables.
- Storing several unrelated values in one attribute, such as a comma-separated list of product IDs.
- Assuming a query returns rows in insertion order.
- Confusing the schema with the current contents of the table.
Exercise
In the customer relation above, is name a tuple, an attribute, the schema, or the relation instance?
Show answer
name is an attribute. It is a named field whose value can differ for each tuple.