How to work through this subject
An order that works, not a checklist. Nothing here is tracked — the steps sequence this subject’s own topics and add none.
- Read what distinguishes a database from a file, and what an RDBMS adds on top.
- Take a small domain and write out its tables, columns and types on paper before touching SQL.
- Mark the primary key of each table, then draw the foreign keys as arrows between them.
- Classify each relationship as one-to-one, one-to-many or many-to-many, and note which one needs a third table.
- Add the constraints that make impossible rows impossible, and say for each what it prevents.
- Finish by explaining what the database guarantees that your application would otherwise have to enforce itself.
What to watch for
- Constraints are not bureaucracy — they are the only thing preventing impossible data.
- Relationship cardinality (one-to-many, many-to-many) determines table shape. Get it wrong and every query fights you.
- A primary key is an identity promise. Without one, you cannot reliably talk about a single row at all.
- NULL is not zero and not an empty string. It is 'unknown', and it propagates through comparisons in ways that surprise people.
- Types are the cheapest constraint you have. Storing a date as text throws away every guarantee the database could have offered.
- Design on paper first. Schema mistakes are the most expensive kind to fix once data exists.
Curriculum topics
- database
- An organised, persistent store of data that many programs can query safely.
- RDBMS
- A relational database management system — the software (like PostgreSQL) that stores tables and enforces their rules.
- table
- A named collection of rows sharing the same columns.
- row
- A single record in a table.
- column
- A named field in a table, with one type shared by every row.
- type
- The kind of value a column accepts — integer, text, timestamp — which the database enforces.
- schema
- The full definition of tables, columns, types and constraints in a database.
- constraint
- A rule the database enforces on the data, such as 'not null' or 'must be unique'.
- primary key
- The column whose value uniquely identifies each row in a table.
- foreign key
- A column pointing at another table's primary key, which the database keeps valid.
- one-to-one
- A relationship where each row on either side matches at most one row on the other.
- one-to-many
- A relationship where one row relates to many rows in another table — one user, many transactions.
- many-to-many
- A relationship where rows on both sides relate to many of the other, requiring a third join table.
Resources for this subject
- REFERENCEPostgreSQL — Data Definition PostgreSQL
The reference for the modelling half of this stage: column types, constraints, keys and what each one actually enforces. Read it while designing the schema, not after the first bad row.Open
Need another explanation?1 more reference
- REFERENCEPostgreSQL Tutorial postgresqltutorial.com
Worked examples for schema design, constraints and psql operation, which the official tutorial covers only briefly.Open
A reviewer should be able to answer these
Answer from memory, out loud, before moving on. These are not tracked and do not affect the gate — they tell you whether you actually understand this subject.
- What does a primary key guarantee, and what breaks without one?
- What does a foreign key enforce, and what happens on delete?
- How do you represent a many-to-many relationship, and why does it need a third table?
- When is NULL meaningful data and when is it a modelling mistake?
- What does the database do for you that application code would have to do otherwise?
- What is the difference between a schema and the tables it contains?
- When would a one-to-one relationship be worth two tables rather than one?
- Why does choosing a column type matter beyond saving space?