Which keys, types and constraints should a new table start with?
The design docs on this site decide which database each kind of data goes in. This page is one level down: the choices inside a table that are cheap to get right on day one and expensive to change at a hundred million rows.
The rest of the page goes deeper, one section per item.
| Choice | Good | Watch out |
|---|---|---|
| bigint identity / AUTO_INCREMENT | Small (8 bytes), ordered, fast inserts at the right edge of the index | Reveals counts and order; hard to generate outside the database |
| UUIDv7 | Made anywhere, roughly time-ordered, so inserts stay at the right edge. uuidv7() is built into PostgreSQL 18 | 16 bytes in every index that carries the key (all secondary indexes in InnoDB) |
| UUIDv4 (random) | Made anywhere, reveals nothing | Every insert lands on a random page: more page splits, a bigger working set, more WAL. Watch an index grow shows it |
| Natural key (email, SKU) | Meaningful | Values change; a changed key cascades through every reference. Make it UNIQUE, not the primary key |
In MongoDB the default _id is an ObjectId, which is already time-ordered. For tenants, put the tenant first in keys and indexes that are always filtered by tenant.
| Data | PostgreSQL | MySQL | MongoDB | Why |
|---|---|---|---|---|
| Money | numeric(12,2) | DECIMAL(12,2) | Decimal128 | Floating point cannot represent 0.10 exactly; sums drift |
| A moment in time | timestamptz | DATETIME(6) in UTC | Date (UTC) | MySQL TIMESTAMP stops at 2038-01-19 |
| Short text | text with a CHECK on length if needed | VARCHAR(n) | string | In PostgreSQL varchar(n) and text perform the same |
| Yes or no | boolean | TINYINT(1)/BOOLEAN | bool | |
| A fixed set of values | a lookup table, or a CHECK | a lookup table | validated string | ENUM types are awkward to change. In MySQL any change rebuilds the table; in PostgreSQL adding a value is cheap, but removing or renaming one is not |
| Flexible attributes | jsonb with a GIN index | JSON with generated columns indexed | native | Keep fields you filter or join on as real columns |
Application checks run one request at a time; two requests at once both pass a "does this email exist?" check and both insert. A UNIQUE constraint does not have that race. The same goes for stock that must not go below zero (a CHECK) and orders that must point at a real customer (a foreign key).
CHECK (quantity > 0).WHERE deleted_at IS NULL, so a deleted row does not block a new one.MongoDB has unique indexes and schema validation ($jsonSchema); it has no foreign keys, so references are checked by the application or not at all.
In a relational database each fact lives in one place, and a join puts them together. Copying a value into another table (denormalizing) makes one read faster and every update of that value more work, and a missed update leaves two answers. I copy only when a measured read needs it, and I write down which table is the source of truth.
| MongoDB: embed when | Reference when |
|---|---|
| The data is read with the parent nearly every time | It is read on its own, or by many parents |
| It is bounded: a few addresses, a handful of line items | It grows without limit: comments, events, messages. A document cannot exceed 16 MB, and big documents are slow to update long before that |
| It changes with the parent | It changes on its own schedule |
The design docs apply this per application: product catalog embeds, chat references.
| Approach | Good for | Cost |
|---|---|---|
| Hard delete | Data with no legal or audit need; personal data on request | Gone; foreign keys decide what happens to children |
Soft delete (deleted_at) | Undo, support tools | Every query and unique index must remember the filter; the table keeps growing |
| History table or event log | Audit, "what did this look like on March 3" | Extra writes; see audit and event logs |
| Partitions dropped by date | Logs, telemetry, anything with retention | Needs partitioning by time from the start; dropping a partition is instant |
A new orders table: id, customer, status, total, created time, and a list of line items. Write down the columns, types and constraints you would start with, in the engine you use.
Write your answer down first. Then open mine.
PostgreSQL: id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, customer_id bigint NOT NULL REFERENCES customers(id) plus an index on it, status text NOT NULL CHECK (status IN ('new','paid','shipped','cancelled')), total numeric(12,2) NOT NULL CHECK (total >= 0), created_at timestamptz NOT NULL DEFAULT now(). Line items in their own table with order_id referencing orders, indexed.
MySQL: the same shape, with BIGINT AUTO_INCREMENT, DECIMAL(12,2) and DATETIME(6) stored in UTC.
MongoDB: one document per order with the line items embedded (bounded, read together, frozen once paid), customer_id as a reference, an index on { customer_id: 1, created_at: -1 }, and a $jsonSchema validator requiring the fields.
The same mechanism, keys and ids, indexes and plans, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. Why are random UUIDv4 primary keys slow for inserts at scale?
Time-ordered keys (identity, UUIDv7, ObjectId) append at the right edge of the index.
2. Two requests check that an email is free and both insert it. What prevents the duplicate?
Only the database sees both inserts at once.
3. Which type for prices?
Floating point cannot represent most decimal fractions exactly.
4. A user's comments can grow without limit. Embed them in the user document?
Unbounded arrays make documents large and slow to update, and documents stop at 16 MB.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.