Templates/SaaS & accounts

Subscription billing database schema

Customers, plans and prices, subscriptions, invoices and payments.

7 tables, 7 relationships

Opens as a new diagram you can change. Free.

Tables

customers

  • iduuid not null
  • emailtext not null
  • nametext
  • external_idtext
  • created_attimestamptz not null

plans

  • iduuid not null
  • nametext not null
  • descriptiontext
  • created_attimestamptz not null

prices

  • iduuid not null
  • plan_id> plansuuid not null
  • currencytext not null
  • amount_centsinteger not null
  • billing_intervaltext not null
  • created_attimestamptz not null

subscriptions

  • iduuid not null
  • customer_id> customersuuid not null
  • price_id> pricesuuid not null
  • statussubscription_status not null
  • current_period_endtimestamptz not null
  • canceled_attimestamptz
  • created_attimestamptz not null

invoices

  • iduuid not null
  • customer_id> customersuuid not null
  • subscription_id> subscriptionsuuid
  • numbertext not null
  • total_centsinteger not null
  • due_attimestamptz not null
  • paid_attimestamptz
  • created_attimestamptz not null

invoice_lines

  • iduuid not null
  • invoice_id> invoicesuuid not null
  • descriptiontext not null
  • quantityinteger not null
  • amount_centsinteger not null
  • created_attimestamptz not null

payments

  • iduuid not null
  • invoice_id> invoicesuuid not null
  • amount_centsinteger not null
  • methodtext not null
  • succeededboolean not null
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TYPE "subscription_status" AS ENUM ('trialing', 'active', 'past_due', 'canceled');

CREATE TABLE "customers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL,
  "name" TEXT,
  "external_id" TEXT UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "invoice_lines" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "invoice_id" UUID NOT NULL,
  "description" TEXT NOT NULL,
  "quantity" INTEGER DEFAULT 1 NOT NULL,
  "amount_cents" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "invoices" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "subscription_id" UUID,
  "number" TEXT NOT NULL UNIQUE,
  "total_cents" INTEGER NOT NULL,
  "due_at" TIMESTAMPTZ NOT NULL,
  "paid_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "payments" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "invoice_id" UUID NOT NULL,
  "amount_cents" INTEGER NOT NULL,
  "method" TEXT NOT NULL,
  "succeeded" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "plans" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "description" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "prices" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "plan_id" UUID NOT NULL,
  "currency" TEXT DEFAULT 'gbp' NOT NULL,
  "amount_cents" INTEGER NOT NULL,
  "billing_interval" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "subscriptions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "price_id" UUID NOT NULL,
  "status" "subscription_status" DEFAULT 'trialing' NOT NULL,
  "current_period_end" TIMESTAMPTZ NOT NULL,
  "canceled_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "prices" ADD CONSTRAINT "fk_prices_plan_id" FOREIGN KEY ("plan_id") REFERENCES "plans" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "subscriptions" ADD CONSTRAINT "fk_subscriptions_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "subscriptions" ADD CONSTRAINT "fk_subscriptions_price_id" FOREIGN KEY ("price_id") REFERENCES "prices" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "invoices" ADD CONSTRAINT "fk_invoices_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "invoices" ADD CONSTRAINT "fk_invoices_subscription_id" FOREIGN KEY ("subscription_id") REFERENCES "subscriptions" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "invoice_lines" ADD CONSTRAINT "fk_invoice_lines_invoice_id" FOREIGN KEY ("invoice_id") REFERENCES "invoices" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "payments" ADD CONSTRAINT "fk_payments_invoice_id" FOREIGN KEY ("invoice_id") REFERENCES "invoices" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.