---
title: "Table Design"
description: "Patterns for designing database tables that stay maintainable as schemas grow."
---

> Documentation Index
> Fetch the complete documentation index at: https://codex.mirio.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Table Design

Patterns for designing tables that stay maintainable as the schema grows:
consistent naming, identities, automatic timestamps, and safe record deletion.

- [Conventions](https://codex.mirio.dev/databases/table-design/conventions/) — Naming rules for tables, columns, indexes, and constraints.
- [Identities](https://codex.mirio.dev/databases/table-design/identities/) — Integer PKs for internal use, UUID and prefixed external identifiers, and encoding choices.
- [Timestamps](https://codex.mirio.dev/databases/table-design/timestamps/) — created_at, updated_at, and auto-updating triggers.
- [Soft Delete](https://codex.mirio.dev/databases/table-design/soft-delete/) — deleted_at, partial indexes, and a live-rows view.

## The canonical table

Putting all the patterns together:

### Postgres

```sql
CREATE TABLE users (
  id         BIGINT      GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email      TEXT        NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  deleted_at TIMESTAMPTZ
);

CREATE UNIQUE INDEX uq_users_email_live ON users (email) WHERE deleted_at IS NULL;

CREATE TRIGGER users_set_updated_at
  BEFORE UPDATE ON users
  FOR EACH ROW
  EXECUTE FUNCTION set_updated_at();

CREATE INDEX idx_users_live ON users (id) WHERE deleted_at IS NULL;
```

### SQLite

```sql
CREATE TABLE users (
  id         INTEGER NOT NULL PRIMARY KEY,
  email      TEXT    NOT NULL,
  created_at TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
  updated_at TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
  deleted_at TEXT
) STRICT;

CREATE UNIQUE INDEX uq_users_email_live ON users (email) WHERE deleted_at IS NULL;

CREATE TRIGGER users_set_updated_at
  AFTER UPDATE ON users
  BEGIN
    UPDATE users SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
    WHERE id = NEW.id;
  END;

CREATE INDEX idx_users_live ON users (id) WHERE deleted_at IS NULL;
```

Source: https://codex.mirio.dev/databases/table-design/index.mdx
