PostgreSQL 中的电商店铺数据库架构
后端开发者可获得一份规范化、可用于生产环境的 DDL 基线,从而直接着手构建 API,无需再为表结构争论不休。
查看输入和输出预览
输入
- 数据要求
- E-commerce platform: users register and place multiple orders. Each order has line items linked to products. Products belong to one or more categories and support variants (size, color) with independent SKUs and stock. Users leave verified reviews with a 1-5 star rating and optional photos. Track inventory levels per variant and order status history.
- 数据库引擎
- PostgreSQL
- 结构化数据样式
- 规范化(3NF)
- 输出格式
- SQL DDL(CREATE TABLE)
- 关系复杂度
- 多对多(连接表)
输出(节选)
CREATE TABLE users (id BIGSERIAL PRIMARY KEY, email CITEXT UNIQUE NOT NULL, created_at TIMESTAMPTZ DEFAULT now()); CREATE TABLE products (id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, base_price NUMERIC(10,2)); CREATE TABLE product_variants (id BIGSERIAL PRIMARY KEY, product_id BIGINT REFERENCES products(id), sku TEXT UNIQUE, size TEXT, color TEXT, stock INT DEFAULT 0); CREATE TABLE product_categories (product_id BIGINT, category_id BIGINT, PRIMARY KEY (product_id, category_id)); -- junction table for many-to-many.