Data modeling case studies: translations, ordering, availability calendars, recurring events, tags and counters
Six common data modeling problems, each with constraints, options, a chosen design and its failure modes, backed by SQL run on PostgreSQL 16 and MySQL 8.4: translations, drag-and-drop ordering, homestay availability, recurring events across DST, tags and click counts.
On this page
1. The problem
A Monday 9:00 Berlin meeting series is generated by adding 7 days to a UTC instant. Tests in Vietnam pass because Asia/Ho_Chi_Minh has no daylight saving. From March 29, 2027, colleagues in Berlin see the meeting jump to 10:00.
2. Key idea
Pick the model that makes the most frequent query cheap while letting the database enforce the constraint you must keep.
| Problem | Main query | Choice | Trap |
|---|---|---|---|
| Translations | read one language, with fallback | JSON column {"vi","en"}; with a workflow, a translation table |
JSON can't tell which translation is stale |
| Drag and drop | ORDER BY position |
numbers with gaps (1000, 2000), new position (above + below) / 2; renumber when out of room |
DOUBLE gives only ~43 halvings before collisions |
| Homestay calendar | "free for 3 nights from Dec 24" | one row per (homestay, day), partial index on free days | booking several nights without checking row count: partial holds |
| Recurring events | "my events this week" | RRULE + local time + IANA, occurrences materialized as timestamptz |
adding days in UTC drifts across DST |
| Tags | tags of one post | an N–N table as the source of truth | array + GIN: renaming a tag rewrites 60,000 rows |
| Click totals | 24-hour total, opened hundreds of times a minute | per-minute rollup table (1.2 ms instead of 200 ms) | HyperLogLog counts distinct users, not totals |
2.1 When does a JSON column fit?
| Use when | Don't use when | |
|---|---|---|
| JSON column | read together, few constraints: displayed translations, partner payloads | money, workflow status, tags with a lifecycle |
| Regular column or separate table | you need FKs, UNIQUE, partial updates, filtering in WHERE |
extension attributes that differ per row |
3. In SQL
-- Wrong: add 7 days to a UTC instant
SELECT ts AT TIME ZONE 'Europe/Berlin'
FROM generate_series(timestamptz '2027-03-15 08:00+00', timestamptz '2027-04-05 08:00+00', interval '7 days') ts;
-- 09:00, 09:00, 10:00, 10:00 ← off by an hour from March 29
-- Right: add 7 days in local time, then convert to UTC
SELECT l AT TIME ZONE 'Europe/Berlin'
FROM generate_series(timestamp '2027-03-15 09:00', timestamp '2027-04-05 09:00', interval '7 days') l;
-- 08:00Z, 08:00Z, 07:00Z, 07:00Z ← always 09:00 in Berlin
-- Homestay: book 3 nights; must be exactly 3 rows, otherwise ROLLBACK
UPDATE homestay_availability SET status = 'BOOKED', booking_id = 101
WHERE homestay_id = 42 AND stay_date >= '2026-12-24' AND stay_date < '2026-12-27'
AND status = 'OPEN';Store a timestamp without offset plus an IANA zone ID (not +07:00).
String keys, recurrence exceptions and rollup tables
- String keys (fractional indexing, LexoRank style): generate a string between two strings; you never run out of room, keys just grow. Compare with collation
"C"(PostgreSQL) orascii_bin(MySQL), or the DB and the app sort differently. - Recurring events need an exceptions table (moved or cancelled occurrences) and a job that extends the window. Nonexistent times (02:30 on the switch day) need a rule: move to the next valid time.
- Per-minute rollup: a job recomputes the last 5 minutes every minute with an upsert, so it's idempotent and catches late clicks.
- Tags: for fast reads add an array column as a denormalized copy, updated from the N–N table.
4. Working with AI
Hand to AI: DDL from the problem statement, realistic sample data (300,000 rows), EXPLAIN ANALYZE comparisons of two options, tests across DST switches.
Check yourself in AI-written code:
- Key constraints live in the database (primary keys, UNIQUE, exclusion), not only in code.
- Multi-row bookings compare the updated row count.
- Recurrences store local time + IANA zone; there's a test in a zone with DST.
- No JSON field appears in
WHEREwithout an index.
5. Exercises
Exercise 1: Review a drag-and-drop position column
The AI designs task(id, list_id, position DOUBLE PRECISION), saying "you can always halve, so you never renumber". Is that true? Propose a replacement for a multi-column Kanban that's safe when two people drag at once.
Solution 1
False: between 1000 and 1001, a double halves only about 43 times before two tasks collide, silently.
CREATE TABLE card (
id bigint PRIMARY KEY,
column_id bigint NOT NULL,
rank text COLLATE "C" NOT NULL,
version int NOT NULL DEFAULT 0,
UNIQUE (column_id, rank)
);
UPDATE card SET column_id = 3, rank = :newRank, version = version + 1
WHERE id = 9 AND version = 5; -- 0 rows: someone else just moved it, reloadUNIQUE (column_id, rank) stops two cards landing in the same slot when two people drop into it at once.
Exercise 2: Choose a meeting-room booking model
Meeting rooms are booked for arbitrary time ranges (9:15–10:40), must not overlap another booking of the same room, and cancelled bookings don't count. The AI proposes reusing the homestay "one row per day" model. Pick a model and explain.
Solution 2
Not one row per day: there's no per-day price, and time ranges don't split evenly. Use ranges with an exclusion constraint:
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE room_booking (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
room_id bigint NOT NULL,
during tstzrange NOT NULL,
status text NOT NULL,
EXCLUDE USING gist (room_id WITH =, during WITH &&) WHERE (status <> 'CANCELLED')
);The database rejects every overlapping pair, even when two requests insert at the same time. MySQL has no exclusion constraints: lock per room (SELECT ... FOR UPDATE on the room row) and check overlaps.
Summary
- Choose the model by the most frequent query and the constraint you must keep.
- Drag and drop: gapped numbers or string keys; never floats.
- Multi-night bookings: a conditional UPDATE and a row-count check.
- Recurring events: local time + IANA, tested across DST switches.
- JSON for data read together; once it's in
WHERE, make it a column.
Further reading: JSON columns, online DDL, data types and views.
Practice interview questions on this topic
- How do you design a schema for multi-language content?
- How do you update counters and click statistics correctly and fast?
- How do you model availability and prevent double booking in a room booking system?
- When should an attribute go into a JSON column, and when into real columns or tables?
- How do you store future appointments and recurring events for a multi-country app?
Comments
Community rules