Uni-Sine
Computer ScienceDatabases

What is a relational database?

What makes a database relational?

The idea comes from Edgar F. Codd, a researcher at IBM, who proposed the relational model in 1970 in a paper called "A Relational Model of Data for Large Shared Data Banks", according to IBM. In his model, data lives in relations, which we now call tables. SQL, the language used to work with relational databases, was developed at IBM in the early 1970s.

  • Table: holds data about one kind of thing, such as customers.
  • Row (record): one item, such as one customer.
  • Column (field): one property, such as a customer's city, with a fixed data type.
  • Primary key: a column whose value is unique for each row.
  • Foreign key: a column that holds another table's primary key, linking the two.

If databases are new to you, start with our guide to what a database is.

A relational database example

An online shop needs to store customers and their orders. Here are two tables:

customer_id (primary key)namecity
1Amira KhanLeeds
2Tom EllisBristol
3Grace ObiLeeds
order_id (primary key)customer_id (foreign key)datetotal
10112 Oct 2026£24.50
10235 Oct 2026£60.00
103119 Oct 2026£12.75

Order 103 doesn't repeat Amira's name and city. It just stores her customer_id, 1. That number is a foreign key: it points to the matching primary key in the customers table. If Amira moves house, you change her city in one row, and every one of her orders is automatically up to date.

Try it: this question shows a table and asks which column should be the primary key.

Choose the primary key column for each table.

Press Mark to have your answer marked in the Databases course.

Types of relationship

customersorders1many
One customer can place many orders; each order belongs to one customer.
RelationshipMeaningExampleHow it is stored
One to oneEach row matches at most one row in the other tableA person and their passportA foreign key that must be unique
One to manyOne row matches many rows in the other tableA customer and their ordersA foreign key on the "many" side
Many to manyMany rows match many rowsStudents and the courses they takeA third, linking table

Many to many relationships can't be stored with a single foreign key, because a student takes many courses and a course has many students. Instead, a linking table (also called a junction table), such as enrolments, has one row per student per course, with two foreign keys: student_id and course_id. That turns one many to many relationship into two one to many relationships. Try one: this question describes two tables and asks for the relationship between them.

Name the relationship type for each scenario.

Press Mark to have your answer marked in the Databases course.

Combining tables with a JOIN

Splitting data into tables is only useful if you can put it back together. In SQL, a JOIN matches rows using the keys. This query lists every order with the customer's name:

SELECT customers.name, orders.order_id, orders.total
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;
nameorder_idtotal
Amira Khan101£24.50
Grace Obi102£60.00
Amira Khan103£12.75

Tom Ellis is missing because he has no orders: an INNER JOIN only keeps rows that match in both tables. A LEFT JOIN keeps every customer, filling in empty (NULL) order details for Tom. Add GROUP BY customers.name with SUM(orders.total) and you get each customer's spending: Amira £37.25 and Grace £60.00. Try one: this question asks which kind of join a task needs.

Pick the JOIN that returns the rows described.

Press Mark to have your answer marked in the Databases course.

Why use a relational database?

  • No needless repeats. Each fact is stored once, which the AQA specification describes as eliminating data redundancy and data inconsistency.
  • Rules the database enforces. Data types, required fields and referential integrity: an order can't point to a customer who doesn't exist.
  • Flexible questions. SQL can combine, filter, sort and total data in ways nobody planned when the tables were designed.
  • Safe changes. Transactions make groups of changes all succeed or all fail together.

Normalisation

Designing tables so each fact is stored once is called normalisation (normalization in the US). The first rule, first normal form, says each cell holds one value: no lists like "Milk, Bread" in a single column. Later normal forms move data that depends on something other than the primary key into its own table, as we did by moving customer details out of the orders table.

Relational vs NoSQL databases

Relational (SQL)NoSQL (for example document databases)
StructureFixed tables and columns, set in a schemaFlexible records whose fields can vary
Linking dataForeign keys and joinsOften stores related data together in one record
Query languageSQLVaries by product
StrengthsConsistency, complex queries, transactionsChanging data shapes, very large scale
ExamplesMySQL, PostgreSQL, SQLite, SQL ServerMongoDB, Redis

A relational database management system (RDBMS) is the software that runs a relational database. The Databases course has you design tables, write joins and compare SQL with NoSQL.

Common mistakes

  • Thinking "relational" means the tables are related. The name comes from Codd's mathematical term "relation", meaning a table. The links between tables are relationships.
  • Putting the foreign key on the wrong side. In a one to many relationship it goes in the "many" table: orders store customer_id, not the other way round.
  • Storing lists in one cell. Use a separate table instead.
  • Trying to link many to many directly. It needs a linking table.
  • Choosing a primary key that can change or repeat, such as an email address or a name.
  • Expecting an INNER JOIN to show unmatched rows. Use a LEFT JOIN for that.

Common questions

What is a relational database in simple terms?
A database that keeps data in separate tables, such as customers and orders, and links them with shared key values, so each fact is stored once.
What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row in its own table. A foreign key is a column in another table that holds those primary key values, creating the link between the two tables.
What is an example of a relational database?
A school system with tables for students, courses and enrolments, linked by student and course IDs. Products that run relational databases include MySQL, PostgreSQL, SQLite and Microsoft SQL Server.
What is the difference between relational and non-relational databases?
Relational databases use fixed tables linked by keys and are queried with SQL. Non-relational (NoSQL) databases use other structures, such as flexible documents or key value pairs.
Why is it called relational?
From the mathematical term relation, which Edgar F. Codd used for a table of rows and columns when he proposed the model in 1970.
Is Excel a relational database?
No. A spreadsheet can look like a table, but it doesn't enforce keys, relationships or data types, and it can't run SQL joins across tables.

Revision cards

Read the front, say your answer, then flip to check.

Where to go next

  • Learn it

    Databases

    SQL, NoSQL and everything in between for storing and querying data

    Start the course
  • Test it

    AP Computer Science Principles

    A new exam-style paper every day, with instant marking and a mark scheme

    Start today's paper
  • Test it

    AP Computer Science A

    A new exam-style paper every day, with instant marking and a mark scheme

    Start today's paper
Read next
Computer Science · DatabasesWhat is a database? Types, software and examplesWhat a database is, how tables, records and fields work, what database software (a DBMS) does, the main types of database and a first SQL query.