Web TechnologiesCourse and practice

Databases

What a database does that a file or a spreadsheet can't, how tables and documents keep the same data in different ways, and how to tell which kind a project needs.

5 parts · about 5 min

Present Cheat sheet

What a database is for

The lesson Where data lives showed the three places where a site keeps data: the company's database, the visitor's browser, and outside services. This lesson opens the first one.

Picture a music shop that sells online. Customers place orders on the site, staff change the stock in the shop, and the accountant reads the invoices, all at the same moment, on the same data. A database does four jobs for them.

It lets many people and programs write at the same moment without losing or doubling anything. It checks rules before saving: a price must be a number, every order needs a customer, two accounts can't share one email address. It finds one record among millions in a fraction of a second, thanks to indexes, which work like the index at the back of a book. And it decides who may read or change what. It also has to be backed up: the lesson No way back explains what makes a backup work.

A spreadsheet or a file can do some of this, for one person at a time.

Cheat sheet

Databasebase de données
an organised store of data on a server, such as every message left in a guestbook.
Recordenregistrement
one item of data in a database, such as one customer or one order.
Indexindex
a sorted list that a database keeps on the side, like the index of a book, to find records without reading them all.
Backupsauvegarde
a copy of a site's data kept in a safe place, to recover it after a loss or an error.

Two ways to keep the same order

Here is the same order at the music shop, kept in two ways.

A table database, such as PostgreSQL or MySQL, or Oracle and SQL Server in many large companies, keeps each kind of thing in its own table: one for customers, one for orders, one for the items in each order. The columns of each table are fixed in advance. Each fact is kept once: Léa's name lives in one row of the customers table, and order 1042 simply points to customer number 7. If she changes her name, it changes in one place and is right everywhere. The database also checks the links: it refuses an order for a customer who doesn't exist.

A document database, such as MongoDB or Firestore, keeps order 1042 as one document, with its customer and its items inside. It's read in one go, and two documents can have different fields. The price of that convenience: Léa's name is copied into each of her orders, so if it changes, every copy must change, and the code must remember to do it.

The same order kept two ways: in linked tables, or as one document1. Tables: the customers, the orders and the items in each order, each in its own table, linked by numbers. 2. A document: the order, its customer and its items, kept together in one piece.Tables, such as PostgreSQL or MySQLEach fact is kept once, in its own tableThe blue numbers link a row to arow in another tableCustomersnumbername7Léa Martin8Karim HaddadOrdersnumbercustomertotal10427€34.3010438€14.50Items in each orderorderproducthow many1042Tuner11042Strings2A document, such as in MongoDBEverything about one order, kept in one piece{ "order": 1042, "customer": "Léa Martin", "items": [ { "product": "Tuner", "how_many": 1 }, { "product": "Strings", "how_many": 2 } ], "total": 34.30}

Swipe sideways to see the whole figure.

Figure 1

Cheat sheet

Table databasebase de données relationnelle
a database that keeps data in tables with fixed columns, linked to each other, such as PostgreSQL or MySQL.
Document databasebase de données orientée documents
a database that keeps each record as one document, whose fields can vary, such as MongoDB or Firestore.
PostgreSQLPostgreSQL
a free, open-source table database, the one developers use most, according to Stack Overflow's survey.
MongoDBMongoDB
the most common document database, first released in 2009.

A question in SQL, and two documents

The example below shows a question in SQL on the left, and two documents on the right.

SQL has been the language of table databases since the 1970s, and it reads almost like English. SELECT name, price says which columns to return, FROM products which table, WHERE price < 20 which rows, and ORDER BY price puts the cheapest first. The answer is three rows: a small table again. It's the question that the four frameworks in Languages and frameworks sent to the database.

On the right, the same kind of products kept as documents. The guitar strings have a gauge, and the tuner has a battery and a clip: each product keeps only the details that make sense for it. In a table, the shop would need a column for every possible detail, empty for most products.

The line between the two kinds is blurring: PostgreSQL can also store documents next to its tables. And nearly every reporting and analysis tool speaks SQL.

A table database answers a question in SQL

Code
The question
SELECT name, price
FROM products
WHERE price < 20
ORDER BY price;
The answer: three rows
name              price
Guitar strings     9.90
Tuner             14.50
Capo              17.00

A document database keeps each product whole

Code
Two products, two shapes
{
  "name": "Guitar strings",
  "price": 9.90,
  "gauge": "medium"
}
{
  "name": "Tuner",
  "price": 14.50,
  "battery": "CR2032",
  "clips_on": true
}
Example 1A question in SQL, and the same kind of products kept as documents

Cheat sheet

SQLSQL
the language used to ask a table database for data, such as every product under €20.
SQL queryrequête SQL
a question sent to a database in SQL, such as "every product under €20, cheapest first".
Fieldchamp
one named piece of information in a record or a document, such as a price or a name.

What each kind is good at

Tables suit linked records that must add up and follow strict rules: invoices, payments and stock, where the totals must be right to the cent. Table databases also offer transactions: several changes saved together, or none of them. A bank transfer is the classic case. The money must leave one account and arrive in the other, or do neither, even if the server stops halfway.

Documents suit records whose shape varies from one to the next and that are read as a whole: a catalogue where each instrument has its own details, a user's settings, a record of events.

Table databases are by far the most common. In Stack Overflow's 2025 survey, about 56% of the developers who answered had used PostgreSQL in the past year, about 41% MySQL, about 24% MongoDB and about 6% Firestore. Among the respondents in France, by our count from the survey's public answers, about two thirds had used PostgreSQL.

Both kinds are mature, so don't choose on fashion. Choose on the data, and early: changing database later means moving all the data and rewriting the code that reads it.

Cheat sheet

Transactiontransaction
several changes to a database saved together or not at all, such as money leaving one account and arriving in another.
Table databasebase de données relationnelle
a database that keeps data in tables with fixed columns, linked to each other, such as PostgreSQL or MySQL.
Document databasebase de données orientée documents
a database that keeps each record as one document, whose fields can vary, such as MongoDB or Firestore.

What to ask

A database usually outlives most of the code around it, so ask these questions early.

Ask what kind of database will hold your data, and why. A good answer talks about your data: whether it must add up, whether each record has its own shape. Ask which rules the database checks by itself, and which ones live only in the code, where they're easier to miss.

Ask whether you can export all your data in a standard format, such as SQL, CSV or JSON, so that you can move to another tool or provider one day. Ask where the database is hosted, and in which country, since personal data comes with GDPR duties. And ask how it's backed up, and when someone last restored a backup.

Cheat sheet

Data exportexport des données
a copy of your data that a service lets you download, so you can move it to another tool.
CSVCSV
a plain-text file of rows and columns, separated by commas, that spreadsheets and databases can read.
GDPRRGPD
the European law on personal data, which applies as soon as a site collects a name or an email.
Backupsauvegarde
a copy of a site's data kept in a safe place, to recover it after a loss or an error.

Practise this strand

These questions put you in the situations this lesson describes. You answer in your own words, and you get a level, feedback and a full answer.