Templates/Finance

Double-entry accounting database schema

Chart of accounts, journals and balanced journal entries per period.

5 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

accounts

  • iduuid not null
  • codetext not null
  • nametext not null
  • kindtext not null
  • parent_id> accountsuuid
  • created_attimestamptz not null

periods

  • iduuid not null
  • nametext not null
  • starts_ondate not null
  • ends_ondate not null
  • closedboolean not null
  • created_attimestamptz not null

journals

  • iduuid not null
  • nametext not null
  • codetext not null
  • created_attimestamptz not null

journal_entries

  • iduuid not null
  • journal_id> journalsuuid not null
  • period_id> periodsuuid not null
  • entry_datedate not null
  • memotext
  • postedboolean not null
  • created_attimestamptz not null

entry_lines

  • iduuid not null
  • entry_id> journal_entriesuuid not null
  • account_id> accountsuuid not null
  • debitnumeric(14,2) not null
  • creditnumeric(14,2) 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 TABLE "accounts" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "code" TEXT NOT NULL UNIQUE,
  "name" TEXT NOT NULL,
  "kind" TEXT NOT NULL,
  "parent_id" UUID,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "entry_lines" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "entry_id" UUID NOT NULL,
  "account_id" UUID NOT NULL,
  "debit" NUMERIC(14,2) DEFAULT 0 NOT NULL,
  "credit" NUMERIC(14,2) DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "journal_entries" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "journal_id" UUID NOT NULL,
  "period_id" UUID NOT NULL,
  "entry_date" DATE NOT NULL,
  "memo" TEXT,
  "posted" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

CREATE TABLE "periods" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "starts_on" DATE NOT NULL,
  "ends_on" DATE NOT NULL,
  "closed" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "accounts" ADD CONSTRAINT "fk_accounts_parent_id" FOREIGN KEY ("parent_id") REFERENCES "accounts" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "journal_entries" ADD CONSTRAINT "fk_journal_entries_journal_id" FOREIGN KEY ("journal_id") REFERENCES "journals" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "journal_entries" ADD CONSTRAINT "fk_journal_entries_period_id" FOREIGN KEY ("period_id") REFERENCES "periods" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "entry_lines" ADD CONSTRAINT "fk_entry_lines_entry_id" FOREIGN KEY ("entry_id") REFERENCES "journal_entries" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "entry_lines" ADD CONSTRAINT "fk_entry_lines_account_id" FOREIGN KEY ("account_id") REFERENCES "accounts" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Finance templates

Start with one diagram, free

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