Sub topic 6
The Relational Model in Brief
This chapter assumes you know basic set notation (subsets, union, cross product), what a table, row and column are, and introductory SQL such as CREATE TABLE and SELECT. It sets up the vocabulary that the chapters on keys, integrity constraints and normal forms rely on.
Why the formal vocabulary matters
SQL talks about tables, rows and columns. GATE questions and database theory use a different vocabulary: relations, tuples and attributes. The two sets of words describe nearly the same things, but the theoretical terms carry rules that tables do not. A relation has no duplicate tuples and no row order. A SQL table can have both unless you forbid them with a key.
| Informal (SQL) term | Formal (relational model) term |
|---|---|
| Table | Relation |
| Row | Tuple |
| Column | Attribute |
| Column data type | Domain |
| Number of columns | Degree (arity) |
| Number of rows | Cardinality |
Domains, attributes and tuples
A domain is a set of allowed atomic values. For example, INT, VARCHAR(50), or the set of valid month numbers {1, ..., 12}. A value is atomic when the model treats it as indivisible. It cannot be a list or a nested table.
An attribute is a named role played by a domain in a relation. Two attributes can share one domain. In a flight table, source and destination might both draw values from the domain of airport codes.
A relation schema R(A1, A2, ..., An) names a relation and lists its attributes. Each attribute Ai has a domain dom(Ai). The degree of R is n.
A tuple t is an ordered list of values <v1, ..., vn> where each vi is in dom(Ai) or is the special value NULL. A relation instance r(R) is a finite set of such tuples. Mathematically, r is a subset of the cross product dom(A1) × dom(A2) × ... × dom(An).
Consider the schema STUDENT(roll, name, dept) and this instance:
| roll | name | dept |
|---|---|---|
| 1 | Asha | CS |
| 2 | Ravi | EE |
| 3 | Asha | CS |
The degree is 3 and the cardinality is 3. Rows 1 and 3 have the same name and department, but they are different tuples because roll differs.
Properties of a relation
Because a relation is a set, four properties follow:
- No duplicate tuples. A set holds each element once. SQL tables relax this and allow duplicates, so SQL works with bags (multisets).
- No ordering of tuples. Rows have no first or last position. Any order you see in a query result is incidental.
- No ordering of attributes in principle. Attributes are identified by name. In the formal definition, a tuple is a mapping from attribute names to values, so reordering columns does not change the relation.
- Atomic values. Each attribute of each tuple holds a single value from its domain. This property is exactly what first normal form later formalises.
NULL
NULL is not a value from any domain. It marks a value that is missing, unknown or not applicable. Three points matter in exams and in practice:
- Any comparison with
NULL, such assalary = NULLorNULL = NULL, evaluates to unknown, not true or false. AWHEREclause keeps a row only when the condition is true. - Use
IS NULLandIS NOT NULLto test for it. - Two
NULLs are not considered equal when checking aUNIQUEconstraint in most systems, so aUNIQUEcolumn can hold severalNULLs. Entity integrity (covered later) forbidsNULLin primary key attributes.
The following SQL shows the effect:
CREATE TABLE student (
roll INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
dept VARCHAR(10)
);
INSERT INTO student VALUES (1, 'Asha', 'CS');
INSERT INTO student VALUES (2, 'Ravi', NULL);
SELECT roll FROM student WHERE dept = 'CS'; -- returns 1
SELECT roll FROM student WHERE dept <> 'CS'; -- returns nothing: NULL <> 'CS' is unknown
SELECT roll FROM student WHERE dept IS NULL; -- returns 2Row 2 appears in neither of the first two queries. This is a common source of wrong answers.
Relation schema versus relation state
The schema is the fixed description: relation name, attributes, domains and constraints. The state (or instance) is the set of tuples stored at one moment. The schema changes rarely. The state changes with every insert, delete or update.
Constraints belong to the schema, but they are checked against every state. A state that satisfies all constraints is a valid state. The database management system (DBMS) rejects any operation that would produce an invalid state.
This distinction is vital for functional dependencies later. An FD such as roll -> name is a statement about the schema, meaning it must hold in every valid state. Looking at one instance and noticing that it holds there does not prove it is a schema-level FD.
A relational database schema
A relational database schema is a set of relation schemas plus a set of integrity constraints. The database state is one instance for each relation. The relations connect to each other through values, not pointers. A column in one relation holds values that identify tuples in another. This later becomes the foreign key.
Common errors
- Calling a relation a list. A relation has no order and no duplicates. Questions that ask "how many tuples" after a projection must account for duplicate removal in the formal model.
- Confusing degree and cardinality. Degree counts attributes. Cardinality counts tuples.
- Treating
NULLas a value. It is not equal to anything, including itself. - Inferring a constraint from one instance. A constraint holds only if every possible valid state obeys it.
- Mixing up cardinality of a relation with cardinality ratio in ER models. The first is a row count. The second is a 1:1, 1:N or M:N relationship property.
Counting possible tuples
A relation with attributes whose domains have sizes d1, d2, ..., dn can hold at most d1 × d2 × ... × dn distinct tuples. Each relation instance is a subset of that product. The number of possible instances is therefore 2 raised to that product.
Example: attributes A with 2 values and B with 3 values give 6 possible tuples and 2^6 = 64 possible instances, including the empty one.
Exercise
A relation R(A, B, C) has |dom(A)| = 3, |dom(B)| = 4 and |dom(C)| = 2. What is its degree, the maximum cardinality of an instance, and why does SELECT roll FROM student WHERE dept <> 'CS' skip a student whose dept is NULL?
Show answer
The degree is 3, the number of attributes. The maximum cardinality is 3 × 4 × 2 = 24 tuples, because an instance is a set and cannot repeat a tuple. The query skips that student because NULL <> 'CS' evaluates to unknown, and a WHERE clause keeps only rows for which the condition is true.
Exercise
Suppose the instance of EMP(id, name) currently shows no two employees with the same name. Can you conclude that name -> id holds as a functional dependency of the schema? Explain.
Show answer
No. A constraint belongs to the schema and must hold in every valid state. One instance with distinct names does not rule out a later valid state with two employees sharing a name but having different ids.