Sub topic 5
The Relational Model in Brief
Assume you know basic set notation, tables, rows, columns, and introductory SQL. This chapter uses those ideas to connect ER modelling with relational database design.
Relations, Tuples, Attributes, and Domains
The relational model represents data as relations. A relation is usually displayed as a table. A tuple is one row in that relation, and an attribute is one named column.
Each attribute has a domain, which is the set of values allowed in that column. For example, salary may have the domain of positive decimal numbers, while dept_id may have the domain of positive integers.
A relation schema describes the structure of a relation:
EMPLOYEE(emp_id, emp_name, salary, dept_id)
The relation itself is the current collection of tuples that follows this schema. In the mathematical model, a relation is a set, so duplicate tuples are not allowed and row order has no meaning. SQL tables can contain duplicates unless a key or another constraint prevents them.
| Term | Meaning | Example |
|---|---|---|
| Relation | A table-shaped set of tuples | `EMPLOYEE` |
| Tuple | One row in a relation | `(101, 'Mina', 70000, 4)` |
| Attribute | A named column | `salary` |
| Domain | Allowed values for an attribute | Positive decimal numbers |
A relation should represent one clear kind of fact. Combining employee, department, and project facts in one large relation often repeats data. Keys and normalization help separate those facts.
Superkeys, Candidate Keys, and Primary Keys
A superkey is a set of one or more attributes that uniquely identifies every tuple in a relation. It may contain unnecessary attributes.
For EMPLOYEE, suppose emp_id is unique and emp_name is not guaranteed to be unique:
{emp_id}is a superkey.{emp_id, emp_name}is also a superkey, but it contains an unnecessary attribute.
A candidate key is a minimal superkey. Minimal means that removing any attribute would destroy uniqueness. If emp_id is the only guaranteed identifier, {emp_id} is a candidate key.
A relation can have several candidate keys. For example, if both emp_id and email are unique, {emp_id} and {email} are candidate keys. The database designer chooses one candidate key as the primary key. The remaining candidate keys are alternate keys.
A primary key must identify each tuple uniquely and cannot be NULL. This is entity integrity. A foreign key is an attribute, or set of attributes, that refers to a key in another relation. It connects related tuples and supports referential integrity.
Exercise
In a relation STUDENT(studentid, email, name), both studentid and email are unique. Which sets are candidate keys, and which key might be selected as the primary key?
Show answer
{student_id} and {email} are candidate keys. Either may be selected as the primary key; the other becomes an alternate key.