Templates/Commerce

Promotions & coupons database schema

Campaigns issuing coupon codes, with redemption tracking against orders.

5 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

campaigns

  • iduuid not null
  • nametext not null
  • starts_attimestamptz not null
  • ends_attimestamptz
  • created_attimestamptz not null

coupons

  • iduuid not null
  • campaign_id> campaignsuuid not null
  • codetext not null
  • percent_offinteger
  • amount_offnumeric(10,2)
  • max_redemptionsinteger
  • created_attimestamptz not null

customers

  • iduuid not null
  • emailtext not null
  • created_attimestamptz not null

orders

  • iduuid not null
  • customer_id> customersuuid not null
  • subtotalnumeric(10,2) not null
  • discountnumeric(10,2) not null
  • created_attimestamptz not null

redemptions

  • iduuid not null
  • coupon_id> couponsuuid not null
  • order_id> ordersuuid not null
  • customer_id> customersuuid 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 "campaigns" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "starts_at" TIMESTAMPTZ NOT NULL,
  "ends_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "coupons" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "campaign_id" UUID NOT NULL,
  "code" TEXT NOT NULL UNIQUE,
  "percent_off" INTEGER,
  "amount_off" NUMERIC(10,2),
  "max_redemptions" INTEGER,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

CREATE TABLE "orders" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "subtotal" NUMERIC(10,2) NOT NULL,
  "discount" NUMERIC(10,2) DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "redemptions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "coupon_id" UUID NOT NULL,
  "order_id" UUID NOT NULL UNIQUE,
  "customer_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "coupons" ADD CONSTRAINT "fk_coupons_campaign_id" FOREIGN KEY ("campaign_id") REFERENCES "campaigns" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "redemptions" ADD CONSTRAINT "fk_redemptions_coupon_id" FOREIGN KEY ("coupon_id") REFERENCES "coupons" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "redemptions" ADD CONSTRAINT "fk_redemptions_order_id" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

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

More Commerce templates

Start with one diagram, free

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