1. Why databases?
Keeping records in separate files (registers, spreadsheets) causes five problems:
- Data redundancy — the same data is stored in many files.
- Data inconsistency — copies disagree after an update is made in only one place.
- Data unavailability — the data exists but is hard to retrieve in the needed form.
- Unauthorised access — anyone who can open a file sees all of it.
- Data dependence — programs depend on the file layout, so changing the layout breaks them.
A database is an organised collection of related data. A DBMS (database management system) is software, such as MySQL, that creates and manages databases: it stores data once and links it, applies rules (keys, constraints), answers queries, controls who can see or change what, and supports backup and sharing.
| Problem | What goes wrong | In the imagined school |
|---|---|---|
| Redundancy | Same data stored many times | Name typed in three files |
| Inconsistency | Copies disagree after an update | New phone number in one file only |
| Unavailability | Data exists but is hard to get | Bus users with unpaid fees takes days |
| Unauthorised access | Anyone with the file sees everything | Fee clerk can read health notes |
| Data dependence | Programs break when the file layout changes | A new column breaks the fee program |
Key terms
- Data redundancy
Unnecessary repetition of the same data in several places.
- Data inconsistency
Copies of the same data that disagree.
- DBMS
Software to create, store, query and control access to a database.
2. The relational data model
In the relational model data is stored in relations (tables). Terms:
- Tuple (record): one row.
- Attribute (field): one named column.
- Domain: the set of values an attribute may take (for example, Age: whole numbers from 0 upwards).
- Degree: the number of attributes.
- Cardinality: the number of tuples (the heading row is not counted).
Properties of a relation: attribute names are unique; each value is atomic (single); no two tuples are identical; the order of tuples and of attributes does not matter.
| Term | Meaning | In STUDENT |
|---|---|---|
| Relation | A table of data | STUDENT |
| Tuple (record) | One row | (2203, Meera Iyer, 12B, LC-107) |
| Attribute (field) | One named column | Class |
| Domain | Set of values an attribute may take | Class: 12A, 12B, 12C, … |
| Degree | Number of attributes | 4 |
| Cardinality | Number of tuples | 5 |
| Property of a relation | Why |
|---|---|
| Each attribute has a unique name | So that every value can be referred to |
| Each value is atomic (single) | One phone number per cell, not a list |
| No two tuples are identical | Each row must be distinguishable |
| Order of tuples does not matter | A relation is a set of rows |
| Order of attributes does not matter | Columns are found by name |
Key terms
- Relation
A table in a relational database.
- Tuple
A row (record) of a relation.
- Attribute
A named column (field) of a relation.
- Domain
The set of values an attribute is allowed to take.
3. Degree, cardinality and the Cartesian product
Degree counts columns and cardinality counts rows. Adding or dropping a column changes the degree; inserting or deleting a row changes the cardinality.
The Cartesian product A × B pairs every tuple of A with every tuple of B:
- degree(A × B) = degree(A) + degree(B)
- cardinality(A × B) = cardinality(A) × cardinality(B)
For the imagined STUDENT (4 columns, 5 rows) and FEES (4 columns, 4 rows), the product has degree 8 and cardinality 20 — measured by running the query on the two tables. Most product rows pair unrelated tuples; keeping only rows whose linking values match gives the meaningful ones.
| Relation (imagined) | Degree | Cardinality |
|---|---|---|
| STUDENT | 4 | 5 |
| FEES | 4 | 4 |
| ITEM | 5 | 6 |
| SUPPLIER | 4 | 3 |
| DOCTOR | 4 | 3 |
| PATIENT | 4 | 5 |
| Step | Degree | Cardinality |
|---|---|---|
| ITEM as given | 5 | 6 |
| Add column Shelf | 6 | 6 |
| Insert two items | 6 | 8 |
| STUDENT | FEES | STUDENT × FEES | |
|---|---|---|---|
| Degree | 4 | 4 | 4 + 4 = 8 |
| Cardinality | 5 | 4 | 5 × 4 = 20 |
Key terms
- Degree
Number of attributes (columns) of a relation.
- Cardinality
Number of tuples (rows) of a relation.
- Cartesian product
All combinations of the tuples of two relations.
4. Candidate, primary and alternate keys
A key lets the DBMS identify exactly one tuple.
- Candidate key: an attribute or set of attributes that is unique for every tuple and never NULL. A relation may have several.
- Primary key: the one candidate key chosen to identify tuples. Exactly one per relation; unique and NOT NULL.
- Alternate key: a candidate key not chosen as the primary key.
- Composite key: a key made of two or more attributes, used when no single attribute is unique, e.g. (AdmNo, Subject) in an exam-result relation.
A good primary key is minimal, simple and stable. Attributes such as names or cities can repeat, so they are not keys even if they look unique in a small sample.
| Attribute of STUDENT | Unique for every row? | Never empty? | Candidate key? |
|---|---|---|---|
| AdmNo | Yes | Yes | Yes |
| Name | No (Kabir Das twice) | Yes | No |
| Class | No (12A twice) | Yes | No |
| LibCard | Yes | Yes | Yes |
| Attribute of SUPPLIER | Unique? | Role |
|---|---|---|
| SupCode | Yes | Primary key (chosen) |
| LicenceNo | Yes | Alternate key |
| SName | Could repeat | Not a key |
| City | Could repeat | Not a key |
| AdmNo | Subject | Marks |
|---|---|---|
| 2201 | Physics | 78 |
| 2201 | English | 84 |
| 2203 | Physics | 91 |
| 2203 | English | 72 |
Key terms
- Candidate key
Attribute(s) unique for every tuple and never empty.
- Primary key
The candidate key chosen to identify tuples of a relation.
- Alternate key
A candidate key that was not chosen as the primary key.
- Composite key
A key made of two or more attributes.
5. Foreign keys and referential integrity
A foreign key is an attribute (or set) in one relation whose values must match the primary key of another relation. It links the two: AdmNo in FEES refers to AdmNo in STUDENT; SupCode in ITEM refers to SUPPLIER; DocID in PATIENT refers to DOCTOR.
Referential integrity means every foreign key value must exist in the referenced relation (or be NULL when there is no link yet). The DBMS therefore refuses:
- a child row whose foreign key matches no parent;
- deleting a parent that child rows still refer to (unless the table is set to cascade).
A foreign key value can repeat (one supplier, many items), and its name need not match the referenced key — only its values and domain must.
| Attempted change | DBMS response | Rule |
|---|---|---|
| New student with AdmNo 2203 | Rejected (UNIQUE constraint failed: STUDENT.AdmNo) | Primary key must be unique |
| New student with LibCard LC-107 | Rejected (UNIQUE constraint failed: STUDENT.LibCard) | Alternate key must be unique |
| Receipt for AdmNo 2209 | Rejected (FOREIGN KEY constraint failed) | Foreign key must match a parent |
| Delete student 2201 | Rejected (FOREIGN KEY constraint failed) | Parent still referred to by receipts |
| Relation | Primary key | Alternate key | Foreign key |
|---|---|---|---|
| DOCTOR | DocID | RegNo | — |
| PATIENT | PID | — | DocID (refers to DOCTOR) |
Key terms
- Foreign key
Attribute(s) whose values must match the primary key of another relation.
- Referential integrity
The rule that every foreign key value refers to an existing tuple (or is NULL).
6. Keys at a glance
Use this table to check answers. In exam answers, name the table with every key, and justify it in a few words: "unique and never empty" for candidate keys, "matches the primary key of …" for foreign keys.
| Key | Unique? | NULL allowed? | How many per table? |
|---|---|---|---|
| Candidate key | Yes | No | One or more |
| Primary key | Yes | No | Exactly one |
| Alternate key | Yes | No (declare NOT NULL) | Zero or more |
| Foreign key | No, may repeat | Yes, if not yet linked | Zero or more |
| Key | One-line rule |
|---|---|
| Candidate key | Any attribute set that is unique and never empty |
| Primary key | The candidate key chosen to identify tuples |
| Alternate key | A candidate key not chosen |
| Foreign key | Values must match the primary key of another table |
| Composite key | A key made of two or more attributes |
Exam tips
Common mistakes
- Counting the heading row in the cardinality; swapping degree and cardinality.
- In a Cartesian product, multiplying the degrees (they add) or adding the cardinalities (they multiply).
- Choosing Name as a key because it looks unique in a small table.
- Calling an alternate key a foreign key, or saying a foreign key must be unique.
- Naming a key without its table: write "AdmNo in FEES is a foreign key referring to STUDENT".
How marks are usually awarded
- 1 mark: the term or the count.
- 2 marks: definition plus the example from the given table.
- 3 marks: three points, each tied to the data or scenario.
- 5 marks: five distinct points, often a comparison or a design with justified keys.