Database design and indexing flashcards
Shape your tables and indexes so data stays right and fast.
Beginner friendly, starts from zero
0
Database design and indexing at a glance
Database design is planning how to store information in tables so it stays correct and is quick to find.
Like a tidy school office: a cabinet for each kind of thing, a card per item with the same boxes, and a roll number on every card.
Words you will meet
- Table
- A sheet that holds one kind of thing, like customers
- Row
- One item in a table, like one customer
- Column
- One detail every row has, like name or phone
- Primary key
- A value unique to each row, like a roll number
- Foreign key
- A column that points to a row in another table
- Constraint
- A rule the database enforces, like 'never empty'
- Index
- A sorted shortcut that finds rows without reading them all
- Schema
- The whole plan of tables, columns and rules
The steps
- 1. Start from zero (12 cards): Tables, rows, keys and links in plain words, then your first table.
- 2. Core basics (15 cards): Entities, keys, relationships, constraints and what an index really is.
- 3. Next steps (16 cards): Normal forms, denormalising on purpose, composite indexes and query plans.
- 4. Go deeper (16 cards): Covering and partial indexes, key choice, partitioning, scaling, history and migrations.
All 12 cards in Start from zero
- What is a database, and what does 'database design' mean?
- A database is an organised store of information that a computer can search fast. Database design is deciding how to arrange that information before you fill it.
- Life before good design: what goes wrong with one giant spreadsheet?
- The same customer's phone number gets typed in 50 rows. Change it in one row and the other 49 are now wrong. Good design writes each fact once, in one place.
- Where do you already meet databases in daily life?
- Your phone contacts, a train ticket booking, your bank statement and a school marks register are all rows in tables inside a database.
- What everyday place works like a well designed database?
- A school office: one cabinet for students, one for classes, a card per student with the same boxes on every card, and a roll number on each. That plan is called the schema.
- What are a table, a row and a column?
- A table holds one kind of thing, like customers. Each row is one item (one customer) and each column is one detail every row has, like phone.
- What is a key, and what are 'primary' and 'foreign' keys?
- A primary key is a value unique to each row, like a roll number. A foreign key is a column that stores another table's primary key to link to it, like a roll number written on a library slip.
- How can tables be linked? One-to-many, many-to-many, one-to-one.
- One restaurant has many dishes (one-to-many). Students take many courses and courses have many students (many-to-many). A person has one passport (one-to-one).
- What do 'type', 'null' and 'constraint' mean?
- A type is the kind of value a column holds: a number, text, a date or true and false. Null means no value at all. A constraint is a rule the database enforces, like 'stock can never be below zero'.
- What are SQL, a query, an index and a scan?
- SQL is the language you use to talk to a database, and a query is one question, like 'find the user with this email'. An index works like the index at the back of a book; without one, the database does a scan, reading every row.
- What do you need before starting?
- For the ideas, only pen and paper. To try the code, install a free database such as PostgreSQL, and learn a little SQL first if you can.
- Your first tiny win: what does this SQL create?
- A new table called
customers.idholds a big whole number and is the primary key, andnameholds text marked NOT NULL, so it can never be left empty. - What will you be able to do after this topic?
- Turn a plain idea for an app into tables, pick keys, link tables the right way, add rules that keep data clean, and add indexes that keep it fast.
Database design and indexing cheatsheet
Keys and links
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEYSurrogate keyid uuid DEFAULT uuidv7()Ordered UUID key (PostgreSQL 18)UNIQUE (email)Natural key ruleREFERENCES customers(id) ON DELETE RESTRICTForeign keyPRIMARY KEY (student_id, course_id)Junction table keyuser_id bigint PRIMARY KEY REFERENCES users(id)One-to-oneCHECK (stock >= 0)Row rule
Normal forms
1NFOne value per cell, no repeating groups2NFNo fact depends on part of a composite key3NFNo fact depends on a non-key columnBCNFEvery determinant is a candidate keyDenormaliseCopy on purpose, with an owner and refresh path
Index types
USING btree (default)Equality, ranges, sortingUSING hashEquality onlyUSING gin (payload jsonb_path_ops)jsonb, arrays, full textUSING gin (name gin_trgm_ops)LIKE '%word%'USING gist (during)Ranges, geometry, overlapsUSING brin (created_at)Huge append-only tablesUSING hnsw (embedding vector_cosine_ops)Vector similarity (pgvector)
Index design
(eq_col, range_col)Equality first, range or sort last(a, b, c) serves a / a,b / a,b,cLeftmost prefix rule... INCLUDE (status, total)Covering, index-only scan... WHERE status = 'queued'Partial indexON users (lower(email))Expression indexCREATE INDEX CONCURRENTLYBuild without blocking writespg_stat_user_indexes.idx_scan = 0Unused, consider dropping
Modelling patterns
amount_minor bigint + currency char(3)Exact moneytimestamptz (UTC)Instantslocal timestamp + IANA zoneFuture local eventsdeleted_at + partial unique indexSoft delete done rightappend-only *_history tableAudit trailtenant_id first + row-level securityMulti-tenant SaaSexpand, migrate, contractZero-downtime schema change