Math · Note 1
Sets, and why a table is one
The marks for membership, subsets, size and the Cartesian product — and the one idea the rest of the series rests on: a database table is a set of tuples, which is what the word relational means.
A set is an unordered collection of distinct things. Write it with curly braces.
Two marks to learn. is said "x is in S", or "x is an element of S", and means that is one of the things in . is said "x is not in S", and means it isn't. So is true, and is true.
Two properties matter enormously later, and they are the reason a set is the right model for a database table.
Unordered. . There is no first element.
No duplicates. is just . An element is either in or out; it cannot be in twice.
The empty set is written . It has nothing in it. It is not nothing — it is a perfectly good set that happens to be empty, the way an empty table is still a table.
Saying which things, instead of listing them
Listing works for four elements. It does not work for four million. So you describe the members by a condition.
Say it: the set of all x in S such that x is greater than 3. The colon means such that. You will also see a vertical bar, , meaning exactly the same thing.
The shape is always the same three parts: where the elements come from, a colon, and which ones you keep. With that set is .
This is the `WHERE` clause. `SELECT * FROM S WHERE x > 3` and are the same thought.
Size
is how many elements a set has — say the size of S, or the cardinality of S.
Put that together with the last section and you already have `COUNT`:
The number of elements of greater than 3. That is `SELECT COUNT(*) FROM S WHERE x > 3`.
One mark more: , said A is a subset of B, means every element of is also in . So is true, and is false.
Tuples, and the Cartesian product
A tuple is an ordered list of things, written with round brackets — . Unlike a set, order matters and duplicates are allowed: . A tuple of two things is a pair, of three a triple, of things an n-tuple.
The Cartesian product , said A cross B, is the set of every pair you can make by taking one element from and one from .
Note the size: . Always. You can cross more than two, and is the set of all triples. This is the `CROSS JOIN`, and it is where the word relational comes from.
A table is a set of tuples
Here is the whole idea, and it is worth pausing on, because everything else is built on it.
A table with columns `(cust_id, name, city)`, where ids are integers and the other two are strings, is a subset of the product of its column domains:
Say it: Customer is a subset of the integers cross string cross string. That one line says every row is a triple; the first slot holds an integer and the other two hold strings; and the table holds some of the possible such triples, not all of them.
A relation is exactly that — a set of tuples. A table is a relation. That is the entire content of the phrase "relational database", and it is why the mathematics of sets applies to it without modification.
The running example
Everything from the third note onwards uses this tiny database. It is worth keeping to hand.
Customer has three rows: , and .
Order has three: , and — the second column is the customer.
Line has four: , , and — order, sku, quantity, unit price.
So , and . Note that Cy has no orders. He is deliberate, and he is how you will find out whether a formula is right.
Exercises
With : is ?
Answer
Yes. Both 3 and 7 are in .
How many elements are in ?
Answer
Two. Duplicates collapse, so it is the set .
Is ?
Answer
Yes. No element of fails to be in , because there is no element of at all. Vacuously true — and genuinely useful rather than a trick.
With , write out , and give its size.
Answer
, and .
What is ?
Answer
. The set is empty and the size of the empty set is zero. Empty cases are where formulas are usually wrong, and they turn up constantly in real data.
If and , what is ? And is the same set as ?
Answer
. And no — its elements are pairs in the other order. The two sets are the same *size*, which is a different claim.
If and , what is ?
Answer
. This is why an unconstrained join across three tables is a bad afternoon.
Write, in set-builder notation, the set of customers in Leeds.
Answer
. Every row is a triple, so you can name its three slots and put the condition on the one you care about. Note 2 introduces , which says the same thing in fewer marks once functions are available.
Questions
What does the set-builder colon mean?
It means 'such that'. In the expression for the set of x in S such that x is greater than three, everything before the colon says where the elements come from and everything after it says which ones to keep. A vertical bar means the same thing.
Why is a database table a set?
Because a table is a collection of rows with no inherent order and no duplicate rows, and each row is a tuple of values drawn from its column types. That makes a table a subset of the Cartesian product of those types, which is the definition of a relation.
What is the difference between a set and a tuple?
A set is unordered and holds no duplicates, written with curly braces. A tuple is ordered and may repeat values, written with round brackets. A table is a set of tuples: the rows have no order, but within a row the columns do.