Course contents · Lesson 5 of 17

What Is a Database? Tables, Rows and SQL Explained

How Apps Are Built · Lesson 5 of 17 · 8 min Foundations

Sign in to a shopping app on a new phone, and your past orders and saved address are all there. They were waiting on the shop's servers, filed so the app could find yours in a moment. This lesson answers "what is a database?", shows how one organises data, and explains why apps don't just use files.

What a database is

A database is an organised collection of data, managed by software that stores, finds and changes it for other programs. In Frontend and Backend it was the store room: the browser asks the backend, and the backend asks the database.

That software is the database management system (DBMS), or database engine; PostgreSQL, MySQL and SQLite are well-known ones.

A database is durable: it keeps data on storage, such as an SSD (solid-state drive), not just in memory, so it survives crashes and power cuts. It is also shared: many programs can change the same data at once, and the engine stops their changes from clashing.

Tables, rows and columns

Most databases you will meet first are relational databases: they keep data in tables that can be linked. Other kinds exist; SQL vs NoSQL compares them next.

A table is a list of one kind of thing, such as books. A row is one item: one book. A column is one attribute every item has, such as its title. Every row has the same columns, like the fields on a catalogue card.

In the diagram, read across a row for one book and down a column for one fact about every book. Notice the highlighted id (identifier) column: each id appears only once.

A books table with columns id, title, author and shelf and three illustrative rows: 1, Pride and Prejudice, Jane Austen, A3; 2, Emma, Jane Austen, A3; 3, Gitanjali, Rabindranath Tagore, T1. Labels say one row is one book and one column is one attribute. The id column is highlighted as the primary key, unique in every row
Each row is one book, each column one fact, and the id tells the rows apart.

Keys: naming rows and linking tables

A primary key is a column, or a set of columns, whose value is unique and never empty, so it names exactly one row: book 2 is Emma. A title would make a poor key, since two books can share one. A good key also never changes, so many tables use a number the database hands out.

Now look at the author column: Jane Austen is typed twice. One typo would look like a new author, and fixing a name means editing every copy.

The fix is a second table. An authors table lists each author once, and each book stores its author's id in an author_id column. A column that holds another table's primary key is a foreign key: it points at that row. In the diagram, follow the arrows.

Two linked tables with illustrative rows. The books table has columns id, title, shelf and author_id; Pride and Prejudice and Emma have author_id 1, Gitanjali has author_id 2. The authors table has columns id and name: 1 is Jane Austen, 2 is Rabindranath Tagore. Arrows run from each author_id, labelled foreign key, to the matching authors id, labelled primary key. A note says each name is stored once, so a fix happens in one place
A foreign key stores another table's primary key, so each name is stored once.

The tables count separately: author_id 1 means author 1, not book 1. Combining linked tables into one answer is a join, taught in SQL Basics.

Why not just save a file?

A program could keep its data in a plain file, such as a spreadsheet saved as CSV (comma-separated values), and for one person's short list that works. A database stores its data in files too, but its engine adds four things.

Fast searching

To find a row in a plain file, a program reads from the top until it finds a match. An index is a separate, sorted list of one column's values, each pointing at its row, so the engine can jump straight there. The cost: it takes space, and every change to the rows must update it too.

No overwriting each other

Two people book the last bus seat at the same moment. With a shared file, both programs read "1 seat left", both write "booked", and the second write replaces the first: one seat, sold twice. A database makes changes to the same row take turns, so the second booking can see that the seat is gone.

A transaction groups changes, such as taking the payment and marking the seat, so they are saved together or not at all.

Rules that keep data valid

Constraints are rules the engine checks on every change: this column is required, this one unique, this price zero or more. A foreign key can be a rule too, refusing a book whose author_id matches no author, whichever program sends it.

Recovery after crashes

If the power fails while a program rewrites a file, the file can end up half old, half new. A database first records each change in a log, often called a write-ahead log, before touching its main files. After a crash, the engine uses it to finish confirmed changes and discard half-done ones.

SQL: how you ask a database

Programs talk to relational databases in SQL (Structured Query Language), the standard language for reading and changing table data. One question is a query, such as SELECT shelf FROM books WHERE title = 'Emma';. Read it aloud: select the shelf, from books, where the title is Emma. The answer is a tiny table: one row, holding A3.

The query never says how to find the row; the engine decides. With a useful index it usually looks the title up, but on a tiny table, reading every row can be just as quick. In the flowchart, notice that both paths give the same answer.

Flowchart of one query. Step 1: the app sends SELECT shelf FROM books WHERE title = 'Emma'. Step 2: the engine reads the query and picks the quickest way. Decision: is an index on title worth using? Yes leads to step 3, use the sorted title index to find Emma in a few steps, then step 4, fetch only that row. No leads to reading every row and comparing each title, slow on a big table. Both paths reach step 5: the result, shelf A3, goes back to the app
SQL says what you want; the engine picks how, and an index makes it fast.

That was a first look; SQL Basics teaches queries properly.

Check yourself

A shop uses each customer's email as the primary key, and every order stores it as a foreign key. A customer changes their email. What goes wrong?

Every order holding the old email must change too, or it points at no customer. A plain id that never changes avoids this: the email becomes an ordinary column, edited in one place.

A booking takes the payment, then the power fails before the seat is marked. Why does it help that both changes were in one transaction?

A transaction is saved whole or not at all, so after the crash the engine discards the half-done booking: no payment is kept without a seat.

An index on title made searches by title fast. Why not index every column?

Every index takes space and must be updated whenever rows are added or changed, so writing gets slower. Index the columns you actually search by.