Natural vs Surrogate Keys
Should the primary key come from the data or be generated? The trade offs, the failure modes of each, and the pattern most teams settle on.
Notes related to SQL.
131 notes tagged
Should the primary key come from the data or be generated? The trade offs, the failure modes of each, and the pattern most teams settle on.
The vocabulary of keys, explained on one table: which columns could identify a row, which one you choose, and what happens to the rest.
What happens to child rows when a parent is deleted or updated, how to choose between RESTRICT, CASCADE and SET NULL, and how to add, drop and disable...
The two constraints that give a database its structure: one that identifies a row, and one that guarantees a reference points at something real.
The four constraints that guard a single table. What each one enforces, how NULL interacts with UNIQUE and CHECK, and why the database is the right pl...
Query a hierarchy of unknown depth, generate a series of rows, or follow a chain of references - all with one self referencing CTE.
A CTE names a query so the rest of the statement can read it like a table. Learn single and chained CTEs, and when a CTE beats a subquery or a view.
The two set operations MySQL waited longest to add: rows in both sets, and rows in one set but not the other - plus how to emulate them where they are...
Stacking two result sets on top of each other. Learn the column compatibility rules, why UNION ALL is faster, and where ORDER BY belongs.
The same question answered four ways, with a straight comparison of readability, NULL safety and cost - and a rule for choosing between them.
Subqueries that reference the outer row and run once per row. Learn EXISTS and NOT EXISTS, why they are NULL safe, and when correlation costs too much...
Subqueries that return a list of values, and the three operators that consume them - including the NULL trap that makes NOT IN return nothing.