The small shop near your house probably keeps a debt book. Every time a customer takes something and pays later, the owner, Mrs. Sri, writes one line: date, name, amount. While the book is thin, this works well. When the book is hundreds of pages thick, two problems appear. To find all of Budi's debts, Mrs. Sri has to turn the pages one by one. And if two customers are both called Budi, their lines are easy to mix up.
A database is where an app keeps its data. A database solves both problems of the debt book. You can search the data quickly, even with millions of rows, and two people with the same name do not get mixed up. The data is also tidy and lasting: it is still there after the app closes or the server (the computer that runs the app) restarts.
Inside, a database is arranged like the filing cabinet in a school office. The cabinet has one folder for each topic: a folder for students, a folder for teachers. Every folder holds many sheets, and every sheet in a folder has the same fields, for example a name. Every student also has a student number that never repeats, even when two students have the same name. Compare the cabinet with a database:
- The cabinet is the database.
- One folder is one table.
- One sheet is one row.
- The fields that every sheet shares are the columns.
- The student number is the primary key.
Tables, rows, columns
This is the users table the exercise uses:
| id | name | age | |
|---|---|---|---|
| 1 | Budi | [email protected] | 25 |
| 2 | Siti | [email protected] | 24 |
| 3 | Andi | [email protected] | 30 |
- Table: a named set of data of one kind. The table above is named
users. - Row: one complete piece of data. The second row holds everything about Siti.
- Column: one kind of information that every row has. This table has 4 columns:
id,name,email,age. Each column has a type, which is the kind of value it holds:ageis always a number andnameis always text.
So this table holds 3 rows and 4 columns. One database usually holds many tables: users, products, orders, and so on.
Primary key: a number that never repeats
A primary key is a column whose value is unique: no two rows have the same value. So one value always points at exactly one row. In the users table, the primary key is the id column.
A name cannot be used to tell rows apart, because two people may both be called Budi. Say there is a second Budi with id 4. The first Budi still has id 1. So to point at one of the two, use id. Mrs. Sri's debt book could do the same: give every customer a number, and the two Budis never get mixed up again.
The database itself makes sure that no id repeats. Say you try to store the second Budi with id 2, when id 2 already belongs to Siti. SQLite (the database program these lessons use) refuses with this message:
Error: UNIQUE constraint failed: users.id
The message means: the values in the id column of the users table must be unique, and that rule was broken. The table still holds 3 rows. This is one difference between a database and a spreadsheet (a sheet in a program like Excel): in a spreadsheet, you can type the same number twice and nothing stops you.
SQL: the language for asking a database
SQL (Structured Query Language) is the standard language for talking to a relational database (a database made of tables). One SQL command is called a query. This is your first query:
SELECT * FROM users;
SELECTmeans "fetch".*means "every column".FROM usersmeans "from theuserstable".;marks the end of the command.
The database answers with a table, called the query's result:
| id | name | age | |
|---|---|---|---|
| 1 | Budi | [email protected] | 25 |
| 2 | Siti | [email protected] | 24 |
| 3 | Andi | [email protected] | 30 |
The result has 3 rows, with the columns id, name, email and age. This query only reads. However many times you run it, the users table does not change.
Command words like SELECT and FROM are usually written in capital letters, so they are easy to tell apart from table and column names. The database also accepts lower case.
If you want only one row, add a condition with WHERE (meaning "the ones that meet the condition"). The query below asks for the row whose id is 2:
SELECT * FROM users WHERE id = 2;
The result is exactly one row, Siti's row. WHERE gets its own lesson, Filtering Data.
Relational databases
A database that stores data in tables (rows and columns) is called a relational database. "Relational" means that its tables can be linked to each other through keys (identifying columns such as id). For example, an orders table has a user_id column. A row whose user_id is 1 is an order from Budi, who has id 1 in the users table.
The relational database programs you will meet most often: SQLite, MySQL and PostgreSQL. All three use SQL, and the basic commands are the same. Only a few things are written a little differently. Later lessons point out each difference when it appears.
When to choose which:
| Database | Good for |
|---|---|
| SQLite | One application whose data is in one file: phone apps, desktop apps, small sites, Cloudflare D1. It needs no server, but only one writer at a time. |
| PostgreSQL | Web applications with many users writing at the same time. A common choice for new projects. |
| MySQL / MariaDB | Many low-cost hosting plans, WordPress, and many Laravel projects. |
On BelajarWeb.dev, SQLite runs your queries right in the browser, so you don't need to install anything.
⚠️ Typos, and the Budi mix-up
- A misspelt table name.
SELECT * FROM user;(no s) is answered withno such table: user: there is no table with that name. A table name has to be written exactly. - A misspelt command word.
SELEC * FROM users;is answered withnear "SELEC": syntax error, which means "there is a writing mistake near SELEC". So look for the typo in that word. - Using a name to point at one person. Names can repeat. Use the primary key (
id).
TL;DR: A database keeps an app's data tidy, lasting and quick to search. A relational database keeps data in tables: one row is one complete piece of data (for example one user), and one column is one kind of information (for example a name). The primary key is the column that identifies a row, usually id. Its value is different in every row, and the database refuses a duplicate. SQL (Structured Query Language) is the language for asking a database. One SQL command is called a query: SELECT * FROM users; fetches every column of every row in the users table.
⚡ Tables, rows, columns
Meaning: A database is where an app keeps its data. A relational database keeps data in tables. One row is one complete piece of data, for example everything about a user called Siti. One column is one kind of information that every row has, such as name or age.
⚡ Primary key: never a duplicate
Guess first: two customers are both called Budi. How does the database tell them apart?
By the primary key, usually the id column. Its value is different in every row: the first Budi has id 1, the second Budi has id 4.
Meaning: A primary key is the column that identifies a row. The database refuses a duplicate primary key value with the error UNIQUE constraint failed: users.id, which names the id column of the users table.
⚡ Your first query
Write:
SELECT * FROM users;
Meaning: This is one query: a command in SQL, the language for asking a database. SELECT means fetch, * means every column, FROM users means from the users table, and ; ends the command. This query only reads, so the table does not change.
⚡ Fetch one row
Write:
SELECT * FROM users WHERE id = 2;
Meaning: WHERE adds a condition. The condition in this query: id must be 2. So the result is exactly one row, Siti's row. SQLite, a database program, runs your query right in the browser.