Relational databases: articles

Databases and storage

Start the SQL practice

Run an article’s SQL in a database of your own.

AI-generated

The relational articles explain their results without requiring a database. Running the SQL lets you change a record or a query and test your own prediction. This guide opens a disposable PostgreSQL instance, then gives each article a fresh database.

The articles follow one tea shop, but each downloadable experiment supplies its own setup and states what it checks. Use that article’s instructions rather than loading an earlier example first. Some downloads require a specific disposable server, account or cluster; a general practice database does not satisfy those guards.

For a first experiment without installing anything, open the SQL sandbox. Each guided lesson prepares its own records. The relational introduction follows the shop purchase through its tables and transactions.

Open a local database

This route uses Docker and PostgreSQL 18. Docker runs the database in a container, a separate environment on your computer; the image supplies PostgreSQL and its psql query client. Have Docker installed and running first. On macOS or Windows, open Docker Desktop and wait for its engine to start; the official installation guide covers installation. On Linux, use a running Docker Engine.

Run this in your computer’s terminal. The first run downloads the PostgreSQL image. It starts a disposable container called staycurrent-practice, with no database port published to your computer. The password belongs only to this practice instance.

docker run --name staycurrent-practice --rm -d -e POSTGRES_PASSWORD=practice-only postgres:18

Check that startup has finished:

docker exec staycurrent-practice pg_isready -h localhost -U postgres

Continue when it reports localhost:5432 - accepting connections. Otherwise, wait briefly and check again. If Docker cannot connect to its daemon, its engine is not ready. If the container name is already in use, keep your existing practice instance or choose another name consistently in these commands.

Create a fresh database for the article

In your terminal, create an empty database and open psql:

docker exec staycurrent-practice createdb -U postgres staycurrent_workshop_1
docker exec -it staycurrent-practice psql -X -U postgres -d staycurrent_workshop_1

The prompt changes to staycurrent_workshop_1=#. You are now talking to PostgreSQL. Enter these commands there:

SELECT current_database();
\pset null 'SQL NULL'

The query should return staycurrent_workshop_1. \pset null makes missing SQL values display as “SQL NULL”; it does not change the records. Commands beginning with a backslash belong to psql and do not need a semicolon. SQL statements such as SELECT do.

Follow the chosen download’s instructions and compare each result before trying a variation. The joins article includes a data-and-queries download using temporary tables. Other articles supply guarded verifiers that require their own isolated fixture rather than this general practice database.

A statement rejected by a constraint may be the intended result. Inspect the error before continuing. Inside an aborted transaction, enter ROLLBACK;, then resume from the article’s stated next step. If setup fails unexpectedly, stop: later statements cannot be assumed to have succeeded.

Start the next article in another empty database

Enter \q in psql to return to your terminal. Create another database in the same container:

docker exec staycurrent-practice createdb -U postgres staycurrent_workshop_2
docker exec -it staycurrent-practice psql -X -U postgres -d staycurrent_workshop_2

Use the next article’s own setup here. Choose another new name for each fresh attempt. This keeps an earlier table, changed record or open transaction from changing the result you are trying to explain.

When a schedule needs clients A and B, both must connect to the same article database. To add a second connection to the first example, open another terminal and run:

docker exec -it staycurrent-practice psql -X -U postgres -d staycurrent_workshop_1

Follow the schedule’s client labels rather than sending every statement on one connection. End both transactions before starting again. Run VACUUM by itself outside a transaction when an example calls for it.

SQLite examples use their separately labelled setup, sometimes an in-memory database created by application code. Use that setup for those examples; PostgreSQL and SQLite syntax is not interchangeable.

Use the native worksheets for operating experiments

A crash, restore or replica experiment needs control of a server and its storage, beyond creating another database. The product branches supply their own isolated environments and list their required binaries, versions and ports:

Follow each worksheet’s setup and cleanup instructions. These experiments own their temporary servers; they do not run inside the Docker database above. The browser lesson catalogue remains another way to explore the ideas, with independent fixtures and explicitly labelled models where a real server operation is unavailable.

Finish the practice

Leave each psql session with \q, then run docker stop staycurrent-practice in your terminal. Because this disposable instance was started with --rm, stopping it removes the container and all its practice databases. Start a new instance for another session.

If you already have PostgreSQL and psql

Use a new empty practice database, then open psql -X -d your_practice_database. Follow your chosen download’s setup and stated requirements. Connection host, account and authentication depend on your installation; the Docker route above supplies one local setup.

Sources and further reading