Skip to the content

Lesson 212 of 296

What a database is: tables, rows and keys

What you will learn

  • What a database and a DBMS are
  • Relational databases: tables, rows and columns
  • Data types
  • Primary keys and foreign keys
  • SQL and its parts
  • The sample database used in this part
On this page

What is a database?

A database is an organised collection of data that is stored permanently and can be searched and changed safely by many users at once. A database management system (DBMS) is the software that does this: PostgreSQL, MySQL, SQL Server, Oracle, SQLite.

Why not just save JSON files? Because a DBMS gives you:

  • Fast searching over millions of records (indexes, Lesson 220).
  • Safe concurrent changes by many users (transactions, Lesson 223).
  • Rules that keep data valid (constraints, Lesson 226).
  • Durability: data survives crashes.
  • A powerful query language.

Relational databases

A relational database stores data in tables, like spreadsheets with strict rules:

  • A table holds one kind of thing: customers, orders.
  • A row (record) is one item: one customer.
  • A column (field) is one property, with a fixed data type: name, email.
idnameemailcity
1Aisha Khanaisha@example.comLondon
2Ben Smithben@example.comLeeds

Tables are related to each other through keys, hence "relational".

Data types (PostgreSQL)

TypeHoldsNotes
integer, bigintWhole numbersUse bigint for IDs of very large tables
numeric(10,2)Exact decimalsFor money if you do not use integer pence
real, double precisionApproximate decimalsNever for money (Lesson 30)
text, varchar(n)TextIn PostgreSQL text is as fast as varchar
booleantrue / false
dateA calendar date
timestamptzA moment in timeAlways use timestamptz (stored as UTC) rather than timestamp without a time zone
uuidA 128-bit unique IDGood for public IDs
jsonbJSON dataFlexible fields; can be indexed
text[]Arrays

Keys

Primary key (PK): a column (or set of columns) that uniquely identifies each row. It can never be NULL or duplicated. Usually an id.

  • A surrogate key is an artificial ID (id 1, 2, 3 or a UUID) with no business meaning. Most tables use one.
  • A natural key is real data that is unique (an email, an ISBN). Natural keys can change (people change emails), so they make poor primary keys, but they should still have a unique constraint.

Foreign key (FK): a column that refers to the primary key of another table. orders.customer_id refers to customers.id. The database then refuses an order for a customer that does not exist, and refuses deleting a customer who still has orders (unless told otherwise). This is called referential integrity.

SQL

SQL (Structured Query Language) is the standard language for relational databases. Its parts:

PartCommandsPurpose
DDL (Data Definition)CREATE, ALTER, DROPDefine tables and structure
DML (Data Manipulation)SELECT, INSERT, UPDATE, DELETERead and change data
DCL (Data Control)GRANT, REVOKEPermissions
TCL (Transaction Control)BEGIN, COMMIT, ROLLBACKTransactions

SQL keywords are not case-sensitive (select works), but writing them in capitals makes queries easier to read. Text values use single quotes: 'London'. Double quotes are for identifiers (table and column names) with special characters.

The sample database

All examples in this part use this small shop:

SQL
CREATE TABLE customers (
  id         integer PRIMARY KEY,
  name       text NOT NULL,
  email      text NOT NULL UNIQUE,
  city       text,
  created_at date NOT NULL
);

CREATE TABLE products (
  id          integer PRIMARY KEY,
  name        text NOT NULL,
  category    text NOT NULL,
  price_pence integer NOT NULL CHECK (price_pence > 0)
);

CREATE TABLE orders (
  id          integer PRIMARY KEY,
  customer_id integer NOT NULL REFERENCES customers(id),
  status      text NOT NULL CHECK (status IN ('pending', 'paid', 'cancelled')),
  ordered_at  date NOT NULL
);

CREATE TABLE order_items (
  order_id         integer NOT NULL REFERENCES orders(id),
  product_id       integer NOT NULL REFERENCES products(id),
  quantity         integer NOT NULL CHECK (quantity > 0),
  unit_price_pence integer NOT NULL,
  PRIMARY KEY (order_id, product_id)
);

The data:

customers

idnameemailcitycreated_at
1Aisha Khanaisha@example.comLondon2026-01-05
2Ben Smithben@example.comLeeds2026-02-11
3Chen Lichen@example.comLondon2026-03-02
4Dara Pateldara@example.comNULL2026-04-20
5Eve Joneseve@example.comManchester2026-05-15

products

idnamecategoryprice_pence
1MugKitchen899
2NotebookStationery450
3Desk LampHome2999
4Pen SetStationery1250
5KettleKitchen3499

orders

idcustomer_idstatusordered_at
1011paid2026-06-01
1021paid2026-06-15
1032cancelled2026-06-20
1043paid2026-07-02
1054pending2026-07-10
1063paid2026-08-01

order_items

order_idproduct_idquantityunit_price_pence
1011 (Mug)2899
1012 (Notebook)1450
1023 (Desk Lamp)12999
1034 (Pen Set)11250
1041 (Mug)1899
1044 (Pen Set)21250
1052 (Notebook)3450
1063 (Desk Lamp)12999
1061 (Mug)1899

Notice the deliberate details: Dara has no city (NULL), Eve has no orders, the Kettle was never ordered, and order_items stores the price at the time of the order (unit_price_pence), because product prices change later. order_items has a composite primary key (two columns together).

How to say it in an interview

"A relational database stores data in tables of rows and typed columns, managed by a DBMS like PostgreSQL that gives fast queries, transactions, constraints and durability. Each table has a primary key that uniquely identifies rows, usually a surrogate ID, and foreign keys reference other tables' primary keys so the database enforces referential integrity. SQL has DDL for structure, DML for data, DCL for permissions and TCL for transactions. I use timestamptz for times, integers or numeric for money, and keep natural identifiers like email unique."

Key words

WordMeaning
Database / DBMSOrganised stored data / the software that manages it
Table, row, columnA kind of thing, one item, one property
Primary keyUniquely identifies each row
Foreign keyReferences a primary key in another table
Referential integrityReferences always point to rows that exist
Surrogate vs natural keyArtificial ID vs meaningful real-world value
Composite keyA key made of several columns

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.