Skip to content
DatabaseChapter 32 / 57Intermediate6 min read

Database Naming & Design

Database naming conventions and design. Use snake_case plural tables, timestamps, foreign keys, enums and indexes to keep data clean and queries fast.

On this page

1. What is this practice?

A database lives longer than code. Code changes every week, but a table name and a column type last for years. So small decisions made early cost a lot later.

2. Why does it matter?

Some real problems that come from weak design:

  • status was free text. Now the table holds Active, active, actv and ACTIVE all together, and report numbers never match.
  • A foreign key was not declared. A user was deleted, their orders were left as "orphans", and the dashboard crashes.
  • on delete cascade was added without thinking. One customer was deleted and all their invoices vanished too, which the law required accounting to keep.
  • timestamp without a timezone. The server is in UTC, the user is in Pakistan, and every report is off by 5 hours.
  • There was no index on the foreign key. With 1,000 rows everything is fine, with a million rows the list page takes 8 seconds.
Without conventionsWith conventions
UserOrders, order_item, tblProduct all mixedorders, order_items, products, one style
Free text status with spelling mistakesEnum or CHECK, only allowed values
Orphan rows with no parentForeign key and a chosen on delete
Timestamp without timezone, hours of differencetimestamptz, always the right time
Big table, slow list queryIndex on every foreign key and filter column

3. When to follow it

LevelHow importantWhy
Learning projectNaming and timestamps at leastA good habit from the start, renaming later is expensive
Production codeRequiredFixing production data needs a migration and carries risk
Senior levelReview keys, delete policy and indexesScale and data integrity are decided here

When to learn it: before you create your first table. Look at the SQL basics in Database Integration.

4. Bad vs good code

Bad

sql
create table "UserOrder" (
  "orderId" serial primary key,
  "userId" int,
  "Status" varchar(50),
  "total" float,
  "createdOn" timestamp
);

Mixed case names need quotes in every query. userId is not a foreign key. Status can hold anything. Money is in float, where 0.1 + 0.2 does not come out right. The timestamp has no timezone, and there is no updated_at at all.

Good

sql
create type order_status as enum ('pending', 'paid', 'shipped', 'cancelled');

create table orders (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references users (id) on delete restrict,
  status order_status not null default 'pending',
  total_cents integer not null check (total_cents >= 0),
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create index orders_user_id_idx on orders (user_id);
create index orders_status_created_at_idx on orders (status, created_at);

What is the difference?

ThingBadGood
Names"UserOrder", "orderId"orders, user_id
Primary keySerial, can be guessed in a URLuuid, because the id goes into the URL
Foreign keyNonereferences users and on delete restrict
StatusFree textorder_status enum
Moneyfloatinteger cents, with a CHECK
Timetimestamp, only createdtimestamptz, created and updated
IndexNoneOn the foreign key and the list query columns

5. How to apply it

Checklist for a new table
Plural snake_case name
Pick a primary key: identity for internal use, uuid for URLs
created_at and updated_at as timestamptz
Declare every foreign key, thinking about on delete
Closed sets in an enum or CHECK
Named indexes on foreign keys and filter columns

A few more conventions:

ThingConvention
Primary keybigint generated always as identity for internal use, uuid when the id appears in a URL
Index name<table>_<columns>_idx, and <table>_<columns>_key for unique
Soft deletedeleted_at only when you need restore or audit, and then every query must respect it
SecretsStore a hash of tokens and codes, never plain text
Status that changes oftenCHECK (status in (...)), because changing an enum is hard

Choose the on delete option with care: restrict (the parent cannot be deleted while children exist), cascade (children are deleted too), set null (the link breaks, the row stays).

6. Exceptions and common mistakes

If you are working with an older database that has its own style, create new tables in that same style. Half camelCase and half snake_case is the worst. Join tables (like project_members) do not need a separate id if a composite primary key is enough, just do not keep both.

Mistake: Putting on delete cascade everywhere

Why? The moment you delete a parent, important history like invoices or audit rows is quietly wiped.

Better: Think about each foreign key: restrict, cascade or set null, and on financial data it is often restrict.

Mistake: Storing money in float or double

Why? Floating point rounding makes totals differ by a few paise, and accounting catches it.

Better: Keep cents or paisa in an integer, or use a numeric type with a fixed scale.

Mistake: Adding indexes only once the site gets slow

Why? By then the table is already big, and creating the index can itself cause locks and downtime.

Better: Add indexes on foreign keys and list query filter columns when you create the table.

Mistake: A soft delete column that some queries forget

Why? Deleted data starts showing up again in search, reports or the API.

Better: Use soft delete only when needed, with a data layer that adds deleted_at is null to every query.

7. What to learn before and after

Final quick revision

CheckQuestion
NamesPlural, snake_case, the same across the whole database?
KeysPrimary key of the right type, every foreign key declared?
On deleteChosen deliberately on every foreign key?
Timecreated_at, updated_at, timestamptz?
Closed setsIs status in an enum or CHECK?
IndexesOn foreign keys and filter columns, with proper names?