Machine Room

8 Database Concepts

Every developer must know them. Table, primary key, foreign key, normalization, join, index, constraints and transactions, all eight met while building one chat app's database. The one-page notes from the reel.

Download the PDF

One-page notes, 8 Database Concepts. Every developer must know them. All eight, met while building one chat app's database. 1, Table. A users table (id, name, email, pw_hash): 1 Maya maya@example.com 7c2e91…, 2 Sam sam@example.com b40f5d…, 3 Maya maya2@example.com e93a1c…. One row for each user, one column for each detail. Store a password hash, never the password. 2, Primary key. A table (id, name): 1 Maya, 2 Sam, 3 Maya, both Mayas circled, the id column boxed and labelled primary key. Two people named Maya: ids 1 and 3 tell them apart. Every row gets its own id that never repeats. 3, Foreign key. A messages table (id, sender_id, content): 1, 1, hi Sam; an arrow from sender_id 1 to the users row 1 Maya; sender_id labelled foreign key. A refused card: sender_id 99 → no user 99 → refused. Each message stores the id of the user who sent it. Deleting Maya while her message exists is refused too. 4, Normalization. Copied (wrong): a table (message id, sender_email) with 1, 2 and 3 at maya@new.example and 4 at maya@example.com, circled and marked missed. She changed her email. One row was missed. Stored once (right): the users row 1 Maya maya@new.example. Messages carry only sender_id 1. One update, every message sees it. Every fact is stored once. 5, Join. The messages row hi Sam, sender_id 1, and the users row id 1, Maya, side by side with an equals sign between sender_id 1 and id 1; the result: content hi Sam, name Maya. SELECT m.content, u.name FROM messages m JOIN users u ON u.id = m.sender_id; A join puts the two tables together on that id. 6, Index. 1,000,000 messages. We want one conversation's 115, in time order. CREATE INDEX messages_conv_time ON messages(conversation_id, created_at); no index: 15.9 ms (reads every row); with the index: 0.013 ms; 1,267× faster. The cost: 200,000 inserts took 102 ms → 200 ms. With it, the database jumps straight to one conversation's messages. Every write updates the index too: index the columns you search by, not every column. Our test: SQLite, one laptop, one run. 7, Constraints. email TEXT NOT NULL UNIQUE. Sign-up with sam@example.com again → refused (UNIQUE). Sign-up with no email → refused (NOT NULL). Rules the database enforces on every write, even if your app has a bug. 8, Transactions. BEGIN, 1. create conversation 8 (done), 2. add its first message (refused), ROLLBACK → conversation 8 is gone. If the message fails, the conversation is rolled back too. All or nothing. Tables hold the data · keys connect them · normalization keeps one copy · joins put it back together · indexes make it fast · constraints keep it correct · transactions keep it whole.

On a phone, press and hold the image to save it. The reel: 8 Database Concepts Every Developer Must Know.