Lesson 1 of 81960s–19707 min

Why databases, and the relational model

In June 1970 Communications of the ACM published a paper by Edgar F. Codd, a mathematician at IBM's research laboratory in San Jose: “A Relational Model of Data for Large Shared Data Banks”. At the time, programs reached their data by navigating, following pointers from a parent record to its children, so every application had to know how the data was laid out.

Codd argued that users should be protected from that layout. Describe the data as plain tables, say what you want, and let the system work out how to fetch it. IBM's System R prototype and Oracle's first commercial product (1979) followed within the decade, and the idea still runs most of the world's business data.

Why files are not enough

Picture a company where sales, billing and shipping each keep their own files. The customer's address is stored three times, so when it changes in one file and not the others the company holds three versions of the truth: redundancy breeds inconsistency. A question nobody planned for (which customers in Elche ordered twice this month?) needs a new program. Rules such as “stock can never be negative” live scattered in application code. Two clerks updating the same record at once can overwrite each other, and a crash halfway through an update leaves the files half changed.

A database management system (DBMS) solves these problems in one place. It holds a single description of the data, the schema; it enforces integrity constraints on every write; it lets many users work concurrently without corrupting each other's changes; it recovers to a consistent state after a crash; and it controls who may read or change what.

Data independence

Codd's central aim was data independence. Physical independence means the storage can change (a new index, a different file layout, a faster disk) without rewriting the programs that use the data. Logical independence means the conceptual schema can grow, say a new column, without breaking views that existing applications depend on. The classic picture has three levels: external views for each user group, one conceptual schema, and the internal storage underneath.

Relations, tuples and keys

In the relational model a relation is a set of tuples over named attributes, and each attribute takes its values from a domain. In everyday terms: a table, its rows and its columns. The number of attributes is the relation's degree; the number of tuples is its cardinality. Because a relation is a set, it has no duplicate tuples and no built-in order of rows. Keys identify tuples:

  • Superkey: any set of attributes whose values are unique for every tuple.
  • Candidate key: a minimal superkey; remove any attribute and it stops being unique.
  • Primary key: the candidate key chosen as the main identifier; it may not be NULL (entity integrity). The other candidates are alternate keys.
  • Foreign key: attributes that must match a key value in another relation, or be NULL (referential integrity).
sql
-- Each department appears once
CREATE TABLE department (
  dept_id  INTEGER PRIMARY KEY,
  name     TEXT NOT NULL UNIQUE
);

-- Each employee belongs to an existing department
CREATE TABLE employee (
  emp_id   INTEGER PRIMARY KEY,            -- primary key
  nif      CHAR(9) NOT NULL UNIQUE,        -- alternate (candidate) key
  name     TEXT NOT NULL,
  dept_id  INTEGER NOT NULL
           REFERENCES department(dept_id)  -- foreign key
);
Keys declared in SQL: the DBMS rejects any row that breaks them.

Worked exercise

Counting keys

The relation Employee(emp_id, nif, name, dept_id) has two attributes that are unique on their own: emp_id and nif. Two employees may share a name and a department. List the candidate keys and count the superkeys.

Try it first, then reveal the solution one step at a time.

Key terms

DBMS
Software that stores data under one schema and enforces integrity, concurrency, recovery and access control.
Relation
A set of tuples over named attributes; a table with no duplicate rows and no inherent order.
Candidate key
A minimal set of attributes that uniquely identifies every tuple.
Foreign key
Attributes whose values must match a key in another relation, or be NULL.
Data independence
Changing storage or schema without rewriting the applications that use the data.

Check what you learned

  1. 1.Which change shows physical data independence?

  2. 2.Course(code, title, credits): code is unique and title is unique. How many candidate keys does it have?

  3. 3.What does a foreign key from employee.dept_id to department.dept_id guarantee?

Practise it for the exam

Sources