Sub topic 1
The relational model: relations, tuples and schemas
Prerequisites
This chapter assumes the following:
- Basic set theory: sets, subsets, Cartesian product, and the fact that a set has no duplicates and no order.
- Elementary logic: AND, OR, NOT, "for all" and "there exists".
- Basic database vocabulary: tables, rows and columns.
Later chapters build relational algebra, tuple calculus and SQL on the definitions given here. Most GATE mistakes about duplicates and tuple counts come from losing track of them.
Relations as sets of tuples
The relational model represents all data as relations. A relation is a set of tuples, and each tuple has the same attributes. This chapter uses the same schema as the rest of the tutorial:
- Student(sid, sname, age)
- Course(cid, cname)
- Enrolled(sid, cid)
Here are the formal terms:
| Term | Meaning | Informal name |
|---|---|---|
| Attribute | A named column, such as `age` | Column |
| Domain | The set of allowed values for an attribute, such as integers | Data type |
| Tuple | An ordered list of values, one per attribute | Row |
| Relation | A set of tuples over the same attributes | Table |
| Degree (arity) | The number of attributes | Column count |
| Cardinality | The number of tuples in the relation | Row count |
Formally, a relation on attributes A1, ..., An with domains D1, ..., Dn is a subset of the Cartesian product D1 × D2 × ... × Dn. A tuple is one element of that product.
Schema versus instance
The relation schema is the relation name with its attributes and their domains. It changes rarely. An instance is the set of tuples stored at one moment, and it changes with every insert, delete or update.
The same distinction applies to the whole database. The database schema is the collection of relation schemas, and a database instance gives one relation instance for each schema.
Properties that follow from "a relation is a set"
Because a relation is a set of tuples, three facts follow. Exam questions test them directly.
- No duplicate tuples. Two tuples that agree on every attribute are the same tuple. A relation therefore contains each tuple at most once.
- No order among tuples. The relation {(1, Asha), (2, Ravi)} equals {(2, Ravi), (1, Asha)}.
- Attribute order is not significant in the formal model, because each value is identified by its attribute name. Positional notation, such as the tuple (1, Asha, 18), is a shorthand that depends on a fixed schema order.
Each value in a tuple is atomic. This means it cannot be a list or a nested relation. Atomic values are what first normal form requires, and they are a precondition for the algebra in later chapters.
Relations versus SQL tables
SQL tables are not exactly relations. A SQL table is a bag (multiset), so it can hold duplicate rows unless a key prevents them. Relational algebra uses set semantics, and SQL uses bag semantics unless you write DISTINCT. Keep these two models separate. Chapter-level questions often hinge on exactly this difference.
Counting tuples
You can bound the number of tuples that a relation can hold:
- The maximum cardinality of a relation is the product of the domain sizes, |D1| × ... × |Dn|, because a relation is a subset of that product.
- The minimum cardinality is 0, since the empty set is a valid relation.
Suppose Student(sid, sname, age) uses sid from {1, 2, 3}, sname from {A, B}, and age from {18, 19}. The product has 3 × 2 × 2 = 12 possible tuples. A legal instance can contain anywhere from 0 to 12 of them. Keys, which come in the next chapter, reduce the maximum further.
NULL: a value outside the domain
Real databases allow the special marker NULL for an unknown or missing value. NULL is not a member of any ordinary domain, and it is not equal to zero or the empty string. It also affects how comparisons behave, which is covered in the SQL NULL chapter. For now, treat NULL as an extension of the pure model. Pure relational algebra and calculus are usually studied without it.
Defining a schema in SQL
The following statements create the schema for Student and load an instance:
CREATE TABLE Student (
sid INT,
sname VARCHAR(50),
age INT
);
INSERT INTO Student (sid, sname, age) VALUES
(1, 'Asha', 18),
(2, 'Ravi', 21),
(3, 'Meena', 22);CREATE TABLE defines the schema. Each INSERT changes the instance. Without a key constraint, SQL would accept a second (1, 'Asha', 18) row. That row would make the table a bag that is no longer a relation in the formal sense.
Common errors
- Treating a table as an ordered list. Relations have no row order, so the "first row" is meaningless without
ORDER BY. - Confusing degree with cardinality. Degree counts columns and cardinality counts rows.
- Assuming duplicates exist in a relation. In relational algebra they do not. In SQL they may.
- Forgetting that the empty relation is valid. It often appears as the answer in minimum-count questions.
- Mixing up schema and instance. A constraint belongs to the schema, while a particular set of rows is an instance.
Exercise
A relation R(A, B, C) has |dom(A)| = 4, |dom(B)| = 3 and |dom(C)| = 5. What are the degree of R, the maximum possible cardinality of an instance, and the minimum possible cardinality?
Show answer
The degree is 3, the number of attributes. The maximum cardinality is 4 × 3 × 5 = 60, because an instance is a subset of the Cartesian product of the domains. The minimum cardinality is 0, since the empty relation is a valid instance.
Exercise
A SQL table T(x) with no constraints contains the rows 1, 1 and 2. How many tuples does the relation that corresponds to SELECT DISTINCT x FROM T contain, and why does it differ from the table's row count?
Show answer
It contains 2 tuples, namely 1 and 2. The table has 3 rows because SQL uses bag semantics and keeps duplicates. A relation is a set, so DISTINCT removes the repeated 1 to match set semantics.