# Sales commission database schema

Source: https://personalsoftwarefactory.com/schemas/commission-calculator
Updated: 2026-12-04

> Seven tables. Deals carry the money: integer cents, an owner and an optional split rep with a whole percentage, in a monthly period that is open or approved. Plans hold marginal tiers as a child table, so the tier math is a pure function of deals, plan and quota. Approved periods are frozen in a JSON snapshot, and anything found later is fixed with a signed adjustment in an open period instead of editing the past.

- 7 tables, 2 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 33 seed rows from the blueprint
- The blueprint's test build passed 24 of 24 acceptance checks

Files: [commission-calculator.sql](https://personalsoftwarefactory.com/schemas/commission-calculator.sql) · [commission-calculator-seed.sql](https://personalsoftwarefactory.com/schemas/commission-calculator-seed.sql) · [the Commission Calculator blueprint](https://personalsoftwarefactory.com/blueprints/commission-calculator)

## Relationships

- plan_tiers.plan_id references plans.id: each plan_tiers row points to exactly one plans row, and one plans row can have many plan_tiers rows pointing to it. On delete: cascade.
- people.plan_id references plans.id: each people row points to at most one plans row, and one plans row can have many people rows pointing to it.
- periods.approved_by references people.id: each periods row points to at most one people row, and one people row can have many periods rows pointing to it.
- deals.period_id references periods.id: each deals row points to exactly one periods row, and one periods row can have many deals rows pointing to it.
- deals.owner_id references people.id: each deals row points to exactly one people row, and one people row can have many deals rows pointing to it.
- deals.split_rep_id references people.id: each deals row points to at most one people row, and one people row can have many deals rows pointing to it.
- deals.created_by references people.id: each deals row points to exactly one people row, and one people row can have many deals rows pointing to it.
- adjustments.period_id references periods.id: each adjustments row points to exactly one periods row, and one periods row can have many adjustments rows pointing to it.
- adjustments.rep_id references people.id: each adjustments row points to exactly one people row, and one people row can have many adjustments rows pointing to it.
- adjustments.deal_id references deals.id: each adjustments row points to at most one deals row, and one deals row can have many adjustments rows pointing to it.
- adjustments.source_period_id references periods.id: each adjustments row points to at most one periods row, and one periods row can have many adjustments rows pointing to it.
- adjustments.created_by references people.id: each adjustments row points to exactly one people row, and one people row can have many adjustments rows pointing to it.
- events.actor_id references people.id: each events row points to exactly one people row, and one people row can have many events rows pointing to it.
- events.period_id references periods.id: each events row points to at most one periods row, and one periods row can have many events rows pointing to it.

## Tables

Also described as: commission tracking database design, sales compensation plan data model, commission calculator database tables.

### plans

A commission plan with a unique name. Implements: Plan.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  | Unique |  |

### plan_tiers

One marginal tier of a plan: where it starts as a percent of quota and its rate in basis points. Implements: child table of Plan.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `plan_id` | TEXT | no |  | Primary key (part); References plans.id, on delete cascade |  |
| `tier` | INTEGER | no |  | Primary key (part) | order in the list; the 1 to 6 range caps a plan at 6 tiers Check: `tier BETWEEN 1 AND 6` |
| `from_pct` | INTEGER | no |  |  | whole percent of quota where the tier starts Check: `from_pct >= 0` |
| `rate_bps` | INTEGER | no |  |  | 0% to 100% Check: `rate_bps BETWEEN 0 AND 10000` |

- Unique together: `plan_id`, `from_pct`
- Table check: `(tier = 1 AND from_pct = 0) OR (tier > 1 AND from_pct > 0)`
- App rule: from_pct increases with tier. A tier starting at 100 or above is an accelerator.

### people

A rep with a plan and monthly quota, or a finance admin. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  | Unique |  |
| `email` | TEXT | no |  |  | the address the person signs in with |
| `title` | TEXT | yes |  |  |  |
| `role` | TEXT | no |  |  | Check: `role IN ('rep', 'finance')` |
| `plan_id` | TEXT | yes |  | References plans.id | reps only; the plan used for open periods |
| `quota_cents` | INTEGER | yes |  |  | reps only; monthly, whole dollars Check: `quota_cents > 0 AND quota_cents % 100 = 0` |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Table check: `role = 'rep' OR (plan_id IS NULL AND quota_cents IS NULL)`
- Unique index `people_email_unique` on (lower(email))

### periods

A calendar month, open or approved, with the statements frozen at approval. Implements: Period.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key | 'YYYY-MM' Check: `length(id) = 7` |
| `status` | TEXT | no | `'open'` |  | Check: `status IN ('open', 'approved')` |
| `approved_by` | TEXT | yes |  | References people.id |  |
| `approved_at` | TIMESTAMP | yes |  |  |  |
| `snapshot` | TEXT | yes |  |  | JSON stored as text: the full statement of every rep, frozen at approval; NULL while open |

- Table check: `(status = 'open' AND approved_by IS NULL AND approved_at IS NULL AND snapshot IS NULL) OR (status = 'approved' AND approved_by IS NOT NULL AND approved_at IS NOT NULL)`
- App rule (R6): periods are approved in order, once; there is no unapprove.

### deals

A closed or cancelled deal in cents, with its owner and optional split rep. Implements: Deal.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | shown as #1001; sequential, never reused Check: `number > 0` |
| `name` | TEXT | no |  |  | Check: `length(name) BETWEEN 1 AND 80` |
| `amount_cents` | INTEGER | no |  |  | at most $10,000,000 Check: `amount_cents > 0 AND amount_cents <= 1000000000` |
| `close_date` | DATE | no |  |  |  |
| `period_id` | TEXT | no |  | References periods.id |  |
| `owner_id` | TEXT | no |  | References people.id | the rep who closed it; gets the amount minus the split credit |
| `split_rep_id` | TEXT | yes |  | References people.id |  |
| `split_pct` | INTEGER | yes |  |  | whole percent credited to split_rep_id, rounded half up to the cent (R2) Check: `split_pct BETWEEN 1 AND 99` |
| `status` | TEXT | no | `'closed'` |  | cancelled is final Check: `status IN ('closed', 'cancelled')` |
| `cancel_reason` | TEXT | yes |  |  |  |
| `cancelled_at` | TIMESTAMP | yes |  |  |  |
| `created_by` | TEXT | no |  | References people.id |  |
| `created_at` | TIMESTAMP | no |  |  |  |

- Table check: `(split_rep_id IS NULL AND split_pct IS NULL) OR (split_rep_id IS NOT NULL AND split_pct IS NOT NULL)`
- Table check: `split_rep_id <> owner_id`
- Table check: `(status = 'closed' AND cancel_reason IS NULL AND cancelled_at IS NULL) OR (status = 'cancelled' AND cancel_reason IS NOT NULL AND cancelled_at IS NOT NULL)`
- Index `deals_period` on (period_id, close_date, number)
- App rule: period_id is the month of close_date, and that period must exist and be open when the deal is added or edited (R7).
- App rule: only reps with a plan and a quota can be credited.

### adjustments

A signed clawback or manual line on a rep's statement that doesn't count toward attainment. Implements: Adjustment.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `period_id` | TEXT | no |  | References periods.id |  |
| `rep_id` | TEXT | no |  | References people.id |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('clawback', 'manual')` |
| `amount_cents` | INTEGER | no |  |  | signed Check: `amount_cents <> 0` |
| `reason` | TEXT | no |  |  | Check: `length(reason) >= 3` |
| `deal_id` | TEXT | yes |  | References deals.id | clawbacks only: the cancelled deal |
| `source_period_id` | TEXT | yes |  | References periods.id | clawbacks only: the approved period it was paid in |
| `created_by` | TEXT | no |  | References people.id |  |
| `created_at` | TIMESTAMP | no |  |  |  |

- Table check: `(kind = 'clawback' AND amount_cents < 0 AND deal_id IS NOT NULL AND source_period_id IS NOT NULL) OR (kind = 'manual' AND deal_id IS NULL AND source_period_id IS NULL)`
- App rule: period_id must be open when the adjustment is created.
- App rule (R10): a clawback is minus exactly what the deal line paid in the approved source period.

### events

One change written as a readable sentence, never edited or deleted. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `at` | TIMESTAMP | no |  |  |  |
| `actor_id` | TEXT | no |  | References people.id |  |
| `period_id` | TEXT | yes |  | References periods.id |  |
| `type` | TEXT | no |  |  | Check: `type IN ('deal_added', 'deal_edited', 'deal_cancelled', 'clawback_created', 'adjustment_added', 'plan_created', 'plan_updated', 'rep_added', 'rep_updated', 'period_opened', 'period_approved')` |
| `text` | TEXT | no |  |  | a readable sentence, e.g. "Clawback -$1,540 for Tom Reed in October 2026: ..." |

## The SQL

```sql
-- Commission Calculator database schema
-- Source: https://personalsoftwarefactory.com/blueprints/commission-calculator (data model, section 5 of the blueprint)
-- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC.
-- License: MIT. Free to copy and adapt.
-- Money is integer cents and rates are integer basis points (1000 = 10%). INTEGER is 32-bit in PostgreSQL,
-- so do tier math (cents x basis points) in the app or in BIGINT, not in INTEGER columns.

-- A commission plan: a name and an ordered list of marginal tiers.
CREATE TABLE plans (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);

-- One tier of a plan (1 to 6 per plan). Each tier runs until the start of the next tier; the last has no end.
-- App rule: from_pct increases with tier. A tier starting at 100 or above is an accelerator.
CREATE TABLE plan_tiers (
  plan_id TEXT NOT NULL REFERENCES plans(id) ON DELETE CASCADE,
  tier INTEGER NOT NULL CHECK (tier BETWEEN 1 AND 6), -- order in the list; the 1 to 6 range caps a plan at 6 tiers
  from_pct INTEGER NOT NULL CHECK (from_pct >= 0), -- whole percent of quota where the tier starts
  rate_bps INTEGER NOT NULL CHECK (rate_bps BETWEEN 0 AND 10000), -- 0% to 100%
  PRIMARY KEY (plan_id, tier),
  UNIQUE (plan_id, from_pct),
  CHECK ((tier = 1 AND from_pct = 0) OR (tier > 1 AND from_pct > 0)) -- the first tier starts at 0
);

-- A person who signs in: a rep on a plan, or a finance admin.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  email TEXT NOT NULL, -- the address the person signs in with
  title TEXT,
  role TEXT NOT NULL CHECK (role IN ('rep', 'finance')),
  plan_id TEXT REFERENCES plans(id), -- reps only; the plan used for open periods
  quota_cents INTEGER CHECK (quota_cents > 0 AND quota_cents % 100 = 0), -- reps only; monthly, whole dollars
  active BOOLEAN NOT NULL DEFAULT TRUE,
  CHECK (role = 'rep' OR (plan_id IS NULL AND quota_cents IS NULL))
);
CREATE UNIQUE INDEX people_email_unique ON people (lower(email)); -- unique, case-insensitive

-- A calendar month of deals. Statements are calculated live while open and frozen in snapshot at approval.
-- App rule (R6): periods are approved in order, once; there is no unapprove.
CREATE TABLE periods (
  id TEXT PRIMARY KEY CHECK (length(id) = 7), -- 'YYYY-MM'
  status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'approved')),
  approved_by TEXT REFERENCES people(id),
  approved_at TIMESTAMP,
  snapshot TEXT, -- JSON stored as text: the full statement of every rep, frozen at approval; NULL while open
  CHECK ((status = 'open' AND approved_by IS NULL AND approved_at IS NULL AND snapshot IS NULL)
      OR (status = 'approved' AND approved_by IS NOT NULL AND approved_at IS NOT NULL))
);

-- A closed deal, credited to its owner or split between the owner and one other rep.
-- App rule: period_id is the month of close_date, and that period must exist and be open when the deal is added or edited (R7).
-- App rule: only reps with a plan and a quota can be credited.
CREATE TABLE deals (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE CHECK (number > 0), -- shown as #1001; sequential, never reused
  name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
  amount_cents INTEGER NOT NULL CHECK (amount_cents > 0 AND amount_cents <= 1000000000), -- at most $10,000,000
  close_date DATE NOT NULL,
  period_id TEXT NOT NULL REFERENCES periods(id),
  owner_id TEXT NOT NULL REFERENCES people(id), -- the rep who closed it; gets the amount minus the split credit
  split_rep_id TEXT REFERENCES people(id),
  split_pct INTEGER CHECK (split_pct BETWEEN 1 AND 99), -- whole percent credited to split_rep_id, rounded half up to the cent (R2)
  status TEXT NOT NULL DEFAULT 'closed' CHECK (status IN ('closed', 'cancelled')), -- cancelled is final
  cancel_reason TEXT,
  cancelled_at TIMESTAMP,
  created_by TEXT NOT NULL REFERENCES people(id),
  created_at TIMESTAMP NOT NULL,
  CHECK ((split_rep_id IS NULL AND split_pct IS NULL) OR (split_rep_id IS NOT NULL AND split_pct IS NOT NULL)),
  CHECK (split_rep_id <> owner_id),
  CHECK ((status = 'closed' AND cancel_reason IS NULL AND cancelled_at IS NULL)
      OR (status = 'cancelled' AND cancel_reason IS NOT NULL AND cancelled_at IS NOT NULL))
);
CREATE INDEX deals_period ON deals (period_id, close_date, number); -- credit order (R1)

-- A signed line on the statement of a rep that does not count toward attainment.
-- App rule: period_id must be open when the adjustment is created.
-- App rule (R10): a clawback is minus exactly what the deal line paid in the approved source period.
CREATE TABLE adjustments (
  id TEXT PRIMARY KEY,
  period_id TEXT NOT NULL REFERENCES periods(id),
  rep_id TEXT NOT NULL REFERENCES people(id),
  kind TEXT NOT NULL CHECK (kind IN ('clawback', 'manual')),
  amount_cents INTEGER NOT NULL CHECK (amount_cents <> 0), -- signed
  reason TEXT NOT NULL CHECK (length(reason) >= 3),
  deal_id TEXT REFERENCES deals(id), -- clawbacks only: the cancelled deal
  source_period_id TEXT REFERENCES periods(id), -- clawbacks only: the approved period it was paid in
  created_by TEXT NOT NULL REFERENCES people(id),
  created_at TIMESTAMP NOT NULL,
  CHECK ((kind = 'clawback' AND amount_cents < 0 AND deal_id IS NOT NULL AND source_period_id IS NOT NULL)
      OR (kind = 'manual' AND deal_id IS NULL AND source_period_id IS NULL))
);

-- One change, in words. Append-only; every change writes exactly one event (R14).
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  at TIMESTAMP NOT NULL,
  actor_id TEXT NOT NULL REFERENCES people(id),
  period_id TEXT REFERENCES periods(id),
  type TEXT NOT NULL CHECK (type IN ('deal_added', 'deal_edited', 'deal_cancelled', 'clawback_created', 'adjustment_added', 'plan_created', 'plan_updated', 'rep_added', 'rep_updated', 'period_opened', 'period_approved')),
  text TEXT NOT NULL -- a readable sentence, e.g. "Clawback -$1,540 for Tom Reed in October 2026: ..."
);
```

## Seed data

Loads section 13: 6 people, 2 plans with 5 tiers, 3 periods, 15 deals and 2 adjustments. Placeholders where the spec gives no value: emails (name@example.com), times (12:00 UTC on the date given), Ana Lima as the creator of deals and the clawback, the SPIF dated 1 Oct, and the deal's cancel reason reused as the clawback reason. Snapshots and events are left out: the spec builds them by running the real actions.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/commission-calculator.sql https://personalsoftwarefactory.com/schemas/commission-calculator-seed.sql | sqlite3 commission_calculator.db
# PostgreSQL
createdb commission_calculator && curl -s https://personalsoftwarefactory.com/schemas/commission-calculator.sql https://personalsoftwarefactory.com/schemas/commission-calculator-seed.sql | psql -v ON_ERROR_STOP=1 commission_calculator
```

## Design decisions

### Integer cents and basis points, with the 32-bit trap named

Section 3 stores money as integer cents and rates as basis points, and R3 rounds only once per tier slice, in integers. INTEGER is 64-bit in SQLite but 32-bit in PostgreSQL, so a $58,000 slice times 1,000 basis points overflows there; the spec's $10,000,000 deal cap keeps stored values in range, and the tier math belongs in the app (the spec asks for a pure function with unit tests) or in BIGINT.

### Tiers are a child table with a position

A plan's tiers are an ordered list of 1 to 6 entries (section 5), so plan_tiers has a tier column checked to 1 to 6, a unique start per plan, and a CHECK that tier 1 starts at 0. Each tier's end is the next tier's start, so it is not stored; storing it would let the two disagree.

### A split is one percentage, and the owner gets the rest

Deals have split_rep_id and split_pct (1 to 99) and CHECKs that both are set or neither, and that the split rep isn't the owner. R2 rounds the split rep's credit half up to the cent and gives the owner amount minus that, so the two always add to the deal; storing two percentages would allow a cent to go missing.

### Approved periods are frozen in a snapshot

periods.snapshot holds every rep's full statement as JSON text from the moment of approval (R7), and a CHECK ties approved_by and approved_at to the status. The build test found that calculating approved August live left out deal #1002, cancelled after approval, giving $8,860 instead of $10,060; reading approved statements from the snapshot, or counting deals cancelled after approval, avoids that. (This point comes from a gap the blueprint's build test found.)

### Clawbacks are adjustments, not edits

Cancelling a deal in an approved period never changes that period (R10). It creates a negative clawback adjustment in an open period for exactly what the deal paid, and a CHECK makes clawbacks negative and requires the deal and the source period, while manual adjustments have neither. Adjustments never change attainment (R5), so they live outside deals.

### Sign-in email is unique ignoring case

The build test found sign-in had to match an email to a Person that had no email field; section 5 now has email, required and unique, case-insensitive. A unique index on lower(email) enforces that on both engines. (This point comes from a gap the blueprint's build test found.)

## Example queries

### Credited bookings and attainment for an open period

```sql
SELECT p.name AS rep, p.quota_cents,
  SUM(c.credit_cents) AS credited_cents,
  SUM(c.credit_cents) * 1000 / p.quota_cents AS attainment_tenths
FROM (SELECT d.id AS deal_id, d.owner_id AS rep_id,
         d.amount_cents - COALESCE((d.amount_cents / 100) * d.split_pct + (2 * (d.amount_cents % 100) * d.split_pct + 100) / 200, 0) AS credit_cents
  FROM deals d
  UNION ALL
  SELECT d.id, d.split_rep_id,
         (d.amount_cents / 100) * d.split_pct + (2 * (d.amount_cents % 100) * d.split_pct + 100) / 200
  FROM deals d
  WHERE d.split_rep_id IS NOT NULL) c
JOIN deals d ON d.id = c.deal_id
JOIN people p ON p.id = c.rep_id
WHERE d.period_id = '2026-10' AND d.status = 'closed'
GROUP BY p.name, p.quota_cents
ORDER BY p.name;
```

On the seed data, for October 2026: 4 rows; Cy Ward has 5,800,000 cents credited, 966 tenths (96.6%).

### Each rep's deal credits in credit order

```sql
SELECT d.number, d.name, d.close_date, p.name AS rep, c.credit_cents
FROM (SELECT d.id AS deal_id, d.owner_id AS rep_id,
         d.amount_cents - COALESCE((d.amount_cents / 100) * d.split_pct + (2 * (d.amount_cents % 100) * d.split_pct + 100) / 200, 0) AS credit_cents
  FROM deals d
  UNION ALL
  SELECT d.id, d.split_rep_id,
         (d.amount_cents / 100) * d.split_pct + (2 * (d.amount_cents % 100) * d.split_pct + 100) / 200
  FROM deals d
  WHERE d.split_rep_id IS NOT NULL) c
JOIN deals d ON d.id = c.deal_id
JOIN people p ON p.id = c.rep_id
WHERE d.period_id = '2026-10' AND d.status = 'closed'
ORDER BY p.name, d.close_date, d.number;
```

On the seed data, for October 2026: 5 rows; Rosa Diaz is credited 1,450,000 cents on #1014 Bluebird new logo.

### Adjustments on October's statements

```sql
SELECT p.name AS rep, a.kind, a.amount_cents, a.reason,
  COALESCE('#' || d.number || ' ' || d.name || ', paid in ' || a.source_period_id, '') AS clawback_of
FROM adjustments a
JOIN people p ON p.id = a.rep_id
LEFT JOIN deals d ON d.id = a.deal_id
WHERE a.period_id = '2026-10'
ORDER BY p.name, a.created_at;
```

On the seed data: 2 rows; Rosa Diaz's clawback of -92,500 cents for #1002 Harbor new logo, paid in 2026-08.

### Plan rate cards with tier ranges

```sql
SELECT pl.name AS plan, t.tier, t.from_pct,
  (SELECT MIN(n.from_pct) FROM plan_tiers n WHERE n.plan_id = t.plan_id AND n.from_pct > t.from_pct) AS to_pct,
  t.rate_bps,
  CASE WHEN t.from_pct >= 100 THEN 'accelerator' ELSE '' END AS label
FROM plan_tiers t
JOIN plans pl ON pl.id = t.plan_id
ORDER BY pl.name, t.tier;
```

On the seed data: 5 rows; AE Standard tier 3 starts at 100% of quota at 1,500 bps and is an accelerator.

### Periods with their approver and deal counts

```sql
SELECT pr.id AS period, pr.status, COALESCE(a.name, '') AS approved_by,
  (SELECT COUNT(*) FROM deals d WHERE d.period_id = pr.id) AS deals,
  (SELECT COUNT(*) FROM deals d WHERE d.period_id = pr.id AND d.status = 'cancelled') AS cancelled_deals
FROM periods pr
LEFT JOIN people a ON a.id = pr.approved_by
ORDER BY pr.id;
```

On the seed data: 3 rows; 2026-08 approved by Ana Lima with 5 deals, 1 of them cancelled.

## What was not tested

- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.

## The blueprint

This schema is section 5 of the [Commission Calculator blueprint](https://personalsoftwarefactory.com/blueprints/commission-calculator), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/commission-calculator/blueprint.md
