A3.1.1 · Database fundamentals
Relational databases
By the end you can
Explain the features, benefits and limitations of a relational database: tables, keys, relationships and why one big table goes wrong.
A is an organised collection of data, stored so that it can be searched, updated and shared. Online shops, social networks, school registers and hospital records all sit on databases. The most common design, and the one this chapter is mostly about, is the relational database: data kept in tables that are linked to each other.
The problem with one big table#
The simplest idea is a : everything in a single table. Here is a bookshop's sales in one:
SALES (flat file)
| Customer | City | Book | Author | AuthorCountry | Qty |
|---|---|---|---|---|---|
| Ana Lopez | Madrid | Tide and Time | Maya Okafor | Nigeria | 2 |
| Ana Lopez | Madrid | Data Detectives | Liam Chen | China | 2 |
| Ben Wright | Leeds | Tide and Time | Maya Okafor | Nigeria | 1 |
| Ben Wright | Leeds | Quiet Algorithms | Arun Patel | India | 2 |
Repeating data wastes space, and worse, it causes three kinds of :
- Update anomaly: if Ana moves to Valencia and you change only some of her rows, the table now disagrees with itself and nothing says which is right.
- Delete anomaly: delete the only sale of a book and you also delete everything known about that book and its author.
- Insert anomaly: you cannot record a new author until somebody buys one of their books, because every row needs a customer.
The cure is to give each kind of thing its own table and link them.
Features of a relational database#
- Tables. A table describes one (a customer, a book). Each row is one (also called a tuple), such as one particular book. Each column is an (or field): one characteristic, such as the price.
- Primary key. One attribute that is different for every row, so each record can be picked out exactly. Names are no good (two books can share a title), so tables usually get an ID.
- Foreign key. An attribute holding another table's primary key. This is how a row in one table points at a row in another.
- Composite key. When no single attribute is unique, two or more together can be (a purchase line identified by order and book). If a key is made by joining pieces of several values (initials, surname and birth date), it is sometimes called a concatenated key.
- Relationships between tables: one-to-one, one-to-many (one author, many books) and many-to-many (customers and books). A many-to-many relationship cannot be stored directly: a third linking table, here Purchase, holds a foreign key to each side, turning it into two one-to-many relationships.
- Integrity rules. means a foreign key must match an existing primary key (or be empty), so a record can never point at something that does not exist.
Benefits#
- Less duplication and redundancy: each fact is stored once, so it takes less space.
- Consistency and integrity: with one copy of each fact, it cannot disagree with itself, and keys and rules stop bad data going in.
- Easy retrieval: SQL (A3.3) can combine and filter tables in one statement.
- Concurrency and reliable transactions: many users can work at once and a transaction either fully happens or not at all (A3.3.6).
- Security: each user can be given only the access they need.
- Scalability and support: relational systems have been used since the 1970s, have huge communities, and are easy to scale up by adding resources to one server.
Limitations#
- Design complexity: splitting data into well-linked tables takes skill, and many tables are harder to understand and maintain.
- Rigid schema: every row must fit the table's columns, so varied or unstructured data (posts, photos, sensor readings with different fields) fits badly, and changing the structure later can be painful.
- Very large data and speed: joining many big tables is slow, and relational systems scale up (a bigger server) more easily than out (many servers). Huge analysis and web-scale workloads often use other designs (A3.4).
- Hierarchical data: deep trees need many joins to walk.
- Object-relational mismatch: programs written with objects (B3) hold data in a different shape from tables, so converting between the two takes effort.
A column holding the price of every book.
One whole row describing a single customer.
The thing the Customer table is about.
Purchase.CustomerID, which holds a value taken from Customer.CustomerID.
BookID, different for every book in the Book table.
OrderID and BookID together identify one line of an order.
0 of 6 matched
Check yourself
Relational databases and keys
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
5 questions · 15 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Describe two anomalies that can happen in a flat file database.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
2.Explain how a primary key and a foreign key are used to create a relationship between two tables.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.State an example of a one-to-many relationship and say which table holds the foreign key.
[2]State: Give a short answer, no explanation needed.
4.Outline two benefits and two limitations of a relational database for a school.
[4]Outline: A brief account: the main points, without much detail.
5.Explain what is meant by referential integrity.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
In a nutshell
- A database is an organised collection of data. A flat file keeps everything in one table, which repeats data and causes insert, update and delete anomalies.
- A relational database stores each kind of thing (entity) in its own table of rows (records or tuples) and columns (attributes or fields), and links tables by keys.
- A primary key uniquely identifies a row (a composite key uses several columns). A foreign key holds another table’s primary key and creates the relationship.
- Relationships are one-to-one, one-to-many or many-to-many. Referential integrity means a foreign key can never point at a row that does not exist.
- Benefits: less redundancy, consistency, integrity, SQL retrieval, concurrency, security. Limitations: design complexity, rigid schema, unstructured data, very large data and hierarchies.
