Chapter 01
Relational model and your setup
What a relational database is, the keys that connect tables, and the server, client and sample data you need before writing real queries.
Relational model
Also called table, row, column, relation, RDBMS, relational database
- What
- Data stored in tables: each table describes one kind of thing, each row is one instance of it and each column is one attribute with a fixed type. Tables are linked through shared key values rather than pointers.
- Why
- Because every fact has one obvious home, you can ask new questions of old data with SQL without changing how it is stored. That flexibility is why relational databases still run most business software.
- Example
- A shop has a customers table, a products table and an orders table. An order row stores customer_id 42 instead of copying the customer name, and a query joins them back together.
Watch out
Treating a table like a spreadsheet tab and packing several facts, such as a comma-separated list of products, into one cell.
Primary key
Also called PK
- What
- A column, or set of columns, whose value is unique and never NULL for every row, so it identifies exactly one row. A table has one primary key.
- Why
- Updates, deletes and relationships all need a reliable way to point at one row. The database also builds a unique index on it, so lookups by key are fast.
Watch out
Using something that looks unique but can change or repeat, such as an email address or a person's name.
Foreign key
Also called FK, referential integrity
- What
- A column that stores the primary key of a row in another table, declared with a constraint so the database rejects values that do not exist there.
- Why
- It is how tables relate, and the constraint stops orphaned rows, such as an order for a customer who was never created or has been deleted.
CREATE TABLE orders (
id int PRIMARY KEY,
customer_id int NOT NULL
REFERENCES customers (id),
placed_at date NOT NULL
);Watch out
Leaving the constraint off "for speed" and later finding thousands of rows that point at nothing.
DDL, DML and DCL
Also called data definition language, data manipulation language, data control language, TCL, DDL, DML, DCL
- What
- The families of SQL statements: DDL defines structure (CREATE, ALTER, DROP), DML reads and changes rows (SELECT, INSERT, UPDATE, DELETE), DCL grants access (GRANT, REVOKE) and transaction control groups changes (COMMIT, ROLLBACK).
- Why
- Knowing which family a statement belongs to tells you its risk. A wrong SELECT wastes a second; a wrong DROP or UPDATE without WHERE can lose data.
Watch out
Assuming DDL can always be rolled back: MySQL commits implicitly on most DDL, while PostgreSQL and SQL Server can roll it back inside a transaction.
Command-line client
Also called psql, mysql client, sqlcmd, CLI
- What
- A terminal program that connects to the database server and runs SQL you type or pipe in: psql for PostgreSQL, mysql for MySQL, sqlcmd for SQL Server.
- Why
- Servers, containers and CI pipelines rarely have a GUI. Running scripts, restoring dumps and checking a production issue all happen in the terminal.
sqlcmd -S localhost -U sa -d shop
SELECT name FROM sys.tables;
GO
-- run a script file from the shell:
-- sqlcmd -S localhost -U sa -d shop -i seed.sqlWatch out
In sqlcmd, forgetting that nothing runs until you type GO on its own line.
SSMS and the VS Code MSSQL extension
Also called SQL Server Management Studio, Azure Data Studio, SSMS, MSSQL extension
- What
- SSMS is the Windows tool for querying and administering SQL Server. On macOS and Linux, and for everyday development anywhere, Microsoft now points to VS Code with the MSSQL extension; Azure Data Studio was retired in February 2026.
- Why
- SSMS shows actual execution plans and handles jobs, backups and security; the VS Code extension keeps queries next to your code on any operating system.
Watch out
Installing Azure Data Studio from an old tutorial: it no longer receives updates or security fixes.
Sample database
Also called pagila, sakila, AdventureWorks, WideWorldImporters
- What
- A ready-made database with realistic tables and thousands of rows that you load locally to practise on, such as pagila for PostgreSQL, sakila for MySQL or AdventureWorks for SQL Server.
- Why
- Toy tables with five rows hide the problems that matter: duplicates, NULLs, joins that multiply rows and queries that are slow at scale.
RESTORE DATABASE AdventureWorks2022
FROM DISK = '/var/opt/mssql/backup/AdventureWorks2022.bak'
WITH MOVE 'AdventureWorks2022'
TO '/var/opt/mssql/data/aw.mdf',
MOVE 'AdventureWorks2022_log'
TO '/var/opt/mssql/data/aw_log.ldf';