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
- E-commerce storeCustomers, products and categories, orders with line items, and addresses.
- Multi-vendor marketplaceSellers with listings, buyer orders split per seller, payouts and reviews.
- Inventory & warehousesStock levels per warehouse, stock movements, suppliers and purchase orders.
- Point of saleStores, registers and staff ringing up sales with mixed payments.
- Restaurant orderingMenus with items and modifiers, and orders for dine-in or takeaway.
- Food deliveryCustomers order from restaurants; couriers pick up, deliver and get rated.
- Product catalogue with variantsProducts with options (size, colour), variants, images and nested categories.
- Subscription boxMonthly boxes, what goes in each, subscribers and their shipments.
- AuctionsAuctions with lots, timed bids, watchers and winners.
- Product catalogue (MongoDB)Products with embedded variants and images, categories and reviews that reference them.
- Orders, single table (DynamoDB)Customers, orders and order lines in one table keyed by PK and SK, with a GSI for orders by status.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.