SQL Databases 00:00:01 Give an order a stable identity A bookshop has customers, orders and order lines. A relational schema gives each fact a place, a type and constraints. The database can reject violations even if two different application servers submit them. The schema comes first: three named columns and a primary key. There are no rows yet. One statement adds Bob and Carol. Ask for the row whose name is Bob. You describe the row, not how to find it. Carol's row is removed. The other rows stay as they were. A table's logical rows are independent of their display order. Ask explicitly for an order whenever the customer needs a deterministic result. 00:00:45 Which customers have no orders? Join orders to their customers by the shared key. Then ask a subtly different question: should customers with no matching order still appear in the result? Orders know only the number of the user. user_id 1 matches users.id 1, so the name is Alice. One user can match many orders. Alice appears twice. Carol has no order, so an inner join does not return Carol. A left join from users would. An inner join answers only with matches. An appropriate outer join retains the unmatched side with missing values. The business meaning determines which query is correct. 00:01:27 Update one address instead of hunting copies A repeated customer field appears on several order rows. Changing only some copies produces conflicting current facts. Move attributes that belong to the customer into one authoritative customer record. Art is written three times because dept_name depends on dept_id, not on the employee. The department is renamed, but one row is forgotten. Now the table disagrees with itself. From the correct table, department facts move to their own table. Employees keep only the key. Rename once. Every employee of department 7 sees it through the join. Sometimes a historical shipping address should remain frozen on an order. Normalize the current customer fact and deliberately snapshot the historical fact when the product really needs both. 00:02:17 Price the lookup and the index maintenance The shop reads by customer email frequently and edits records all day. An index can reduce the rows inspected for the lookup, but it consumes storage and adds work when indexed values change. 100,000,000 rows at 50 a page are 2,000,000 pages. Any of them may hold the row, so all are read. A sorted tree of emails, each with the place of its row. The base 500 logarithm of 100 million is just under 3, so three levels. Three index pages, then one table page. From now on every new user also adds an index entry. This is worth it for a login; it may not be for a column nobody filters on. Choose indexes for actual predicates and ordering, then inspect plans with realistic distributions. A small or weakly selective query may still favor a scan. 00:03:14 Sell one seat once Two customers want the last seat. Both can observe that it was available before either transaction commits. The application therefore needs a rule that the database enforces at the commit boundary. Each server checks, and each sees seat 12 free. The check alone protects nothing. The table has a unique rule on show and seat. The second insert waits for the first. Person 1 commits. Person 2 gets a unique violation, and the app says the seat is taken. A unique constraint on the correct event and seat identity or a conditional update can select one winner. Record the resulting refusal clearly instead of issuing two apparently valid tickets.