Templates/Commerce

E-commerce store database schema

Customers, products and categories, orders with line items, and addresses.

6 tables, 7 relationships

Opens as a new diagram you can change. Free.

Tables

customers

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

addresses

  • iduuid not null
  • customer_id> customersuuid not null
  • line1text not null
  • line2text
  • citytext not null
  • postcodetext not null
  • countrytext not null
  • created_attimestamptz not null

categories

  • iduuid not null
  • nametext not null
  • slugtext not null
  • parent_id> categoriesuuid
  • created_attimestamptz not null

products

  • iduuid not null
  • category_id> categoriesuuid
  • nametext not null
  • skutext not null
  • pricenumeric(10,2) not null
  • stockinteger not null
  • created_attimestamptz not null

orders

  • iduuid not null
  • customer_id> customersuuid not null
  • shipping_address_id> addressesuuid
  • statusorder_status not null
  • totalnumeric(10,2) not null
  • created_attimestamptz not null

order_items

  • iduuid not null
  • order_id> ordersuuid not null
  • product_id> productsuuid not null
  • quantityinteger not null
  • unit_pricenumeric(10,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 TYPE "order_status" AS ENUM ('pending', 'paid', 'shipped', 'delivered', 'cancelled');

CREATE TABLE "addresses" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "line1" TEXT NOT NULL,
  "line2" TEXT,
  "city" TEXT NOT NULL,
  "postcode" TEXT NOT NULL,
  "country" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "categories" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "slug" TEXT NOT NULL UNIQUE,
  "parent_id" UUID,
  "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,
  "name" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "order_items" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "order_id" UUID NOT NULL,
  "product_id" UUID NOT NULL,
  "quantity" INTEGER NOT NULL,
  "unit_price" NUMERIC(10,2) NOT NULL,
  "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,
  "shipping_address_id" UUID,
  "status" "order_status" DEFAULT 'pending' NOT NULL,
  "total" NUMERIC(10,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "products" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "category_id" UUID,
  "name" TEXT NOT NULL,
  "sku" TEXT NOT NULL UNIQUE,
  "price" NUMERIC(10,2) NOT NULL,
  "stock" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "addresses" ADD CONSTRAINT "fk_addresses_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "products" ADD CONSTRAINT "fk_products_category_id" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE SET NULL 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 "orders" ADD CONSTRAINT "fk_orders_shipping_address_id" FOREIGN KEY ("shipping_address_id") REFERENCES "addresses" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "order_items" ADD CONSTRAINT "fk_order_items_order_id" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "order_items" ADD CONSTRAINT "fk_order_items_product_id" FOREIGN KEY ("product_id") REFERENCES "products" ("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.