// Define enums
Enum order_status {
pending
confirmed
processing
shipped
delivered
cancelled
}
Enum user_role {
admin
seller
customer
}
// Users table
Table users {
id int [pk, increment]
email varchar(255) [unique, not null]
name varchar(100) [not null]
role user_role [default: 'customer']
created_at timestamp [default: `now()`]
Note: 'User accounts for the e-commerce platform'
}
// Products table
Table products {
id int [pk, increment]
sku varchar(50) [unique, not null]
name varchar(255) [not null]
description text
price decimal(10,2) [not null, check: `price > 0`]
stock_quantity int [default: 0, check: `stock_quantity >= 0`]
categories varchar(50)[]
tags text[]
metadata jsonb
created_at timestamp [default: `now()`]
Indexes {
sku [unique, name: 'idx_products_sku']
categories [type: gin, name: 'idx_products_categories']
}
checks {
`price > 0` [name: 'check_positive_price']
`stock_quantity >= 0` [name: 'check_non_negative_stock']
}
}
// Orders table
Table orders {
id int [pk, increment]
user_id int [not null, ref: > users.id]
status order_status [default: 'pending']
total_amount decimal(10,2) [not null]
shipping_address text
created_at timestamp [default: `now()`]
Indexes {
(user_id, created_at) [name: 'idx_orders_user_time']
status [name: 'idx_orders_status']
}
checks {
`total_amount > 0` [name: 'check_positive_total']
}
}
// Order items table
Table order_items {
order_id int [not null]
product_id int [not null]
quantity int [not null, check: `quantity > 0`]
unit_price decimal(10,2) [not null, check: `unit_price > 0`]
Indexes {
(order_id, product_id) [pk, name: 'pk_order_items']
}
}
// Reviews table
Table reviews {
id int [pk, increment]
product_id int [not null, ref: > products.id]
user_id int [not null, ref: > users.id]
rating int [not null, check: `rating >= 1 AND rating <= 5`]
comment text
created_at timestamp [default: `now()`]
Indexes {
(product_id, user_id) [unique, name: 'idx_reviews_product_user']
}
}
// Define relationships
Ref: order_items.order_id > orders.id
Ref: order_items.product_id > products.id