The tables
Ten tables in four groups: the customers, the catalogue, the cart and the orders.
dbo.CustomerOne row per account, found by email.dbo.AddressA customer's saved addresses; at most one is the default.dbo.CategoryProduct categories by name, each inside a parent category or at the top.dbo.ProductOne row per product, found by SKU, with its category and current price.dbo.CartA customer's cart, at most one, and when it last changed.dbo.CartItemA product in the cart and how many; its key is the pair.dbo.CustomerOrderAn order: its number, the ship-to address as it was, when it was placed and its status.dbo.OrderItemA product on an order, the quantity and the price paid; its key is the pair.dbo.PaymentMoney taken for an order: when, how much and how.dbo.ReviewA customer's rating of a product, 1 to 5, with optional text.
How the tables relate
- A customer has zero or more addresses , orders and reviews; each of those belongs to exactly one customer: its
CustomerIdis NOT NULL, so the line has nothing at the customer’s end . - A customer has zero or one cart : the cart’s primary key is
CustomerId, so a second cart for the same customer fails. - Orders and products are many-to-many:
dbo.OrderItemholds one row per pair, keyed by both columns, a composite key.dbo.CartItemanddbo.Revieware built the same way, so in crow’s foot notation their lines are solid and their boxes have rounded corners . - A category has zero or one parent category:
ParentCategoryIdreferencesdbo.Categoryitself and allows NULL, so Headphones can sit under Electronics. - An order has zero or more payments, so an order paid partly with a gift card and partly by card is two rows.
Why it is built this way
An order keeps the address it shipped to
dbo.CustomerOrder copies the ship-to address into its own columns, ShipLine1 to ShipCountryCode, instead of pointing at dbo.Address. A customer who moves edits or deletes an address, and every past order still shows where it went. The saved addresses only fill the checkout form.
An order keeps the price it was sold at
dbo.OrderItem.UnitPrice copies the product’s price when the order is placed. Prices change; an order’s total doesn’t. Totals are summed from the order’s own lines, never from dbo.Product.Price:
SELECT o.OrderNumber, o.PlacedAt, SUM(i.Quantity * i.UnitPrice) AS Total FROM dbo.CustomerOrder AS o JOIN dbo.OrderItem AS i ON i.CustomerOrderId = o.CustomerOrderId WHERE o.CustomerId = @CustomerId GROUP BY o.OrderNumber, o.PlacedAt ORDER BY o.PlacedAt DESC;
One default address, one cart, one review
A filtered unique index on dbo.Address (CustomerId) WHERE IsDefault = 1 lets a customer save many addresses and mark one as the default; a second default fails with Msg 2601. The cart’s primary key refuses a second cart with Msg 2627, and the review’s primary key (ProductId, CustomerId) refuses a second review of the same product by the same customer with Msg 2627 as well. A rating of 6 fails CK_Review_Rating with Msg 547.
A product is on an order once
The primary key (CustomerOrderId, ProductId) of dbo.OrderItem makes a second line for the same product fail with Msg 2627: two of a product is one line with a quantity of 2. The cart’s lines follow the same rule.
Order, a reserved word
ORDER is a reserved word in SQL Server, so a table named Order must be quoted, [Order], in every statement that names it. The order table is dbo.CustomerOrder instead, and its key CustomerOrderId.
The SQL Server script
-- Table [dbo].[Category] CREATE TABLE [dbo].[Category] ( [CategoryId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (100) NOT NULL, [ParentCategoryId] INT NULL, CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED ([CategoryId] ASC), CONSTRAINT [FK_Category_Category_ParentCategoryId] FOREIGN KEY ([ParentCategoryId]) REFERENCES [dbo].[Category] ([CategoryId]), CONSTRAINT [AK_Category_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Customer] CREATE TABLE [dbo].[Customer] ( [CustomerId] INT IDENTITY (1, 1) NOT NULL, [Email] NVARCHAR (254) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [JoinedOn] DATE NOT NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ([CustomerId] ASC), CONSTRAINT [AK_Customer_Email] UNIQUE NONCLUSTERED ([Email] ASC) ); GO -- Table [dbo].[Address] CREATE TABLE [dbo].[Address] ( [AddressId] INT IDENTITY (1, 1) NOT NULL, [CustomerId] INT NOT NULL, [Line1] NVARCHAR (200) NOT NULL, [Line2] NVARCHAR (200) NULL, [City] NVARCHAR (100) NOT NULL, [PostalCode] VARCHAR (20) NOT NULL, [CountryCode] CHAR (2) NOT NULL, [IsDefault] BIT NOT NULL, CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED ([AddressId] ASC), CONSTRAINT [FK_Address_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]) ); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_Address_CustomerId_Default] ON [dbo].[Address]([CustomerId] ASC) WHERE (IsDefault = 1); GO -- Table [dbo].[Cart] CREATE TABLE [dbo].[Cart] ( [CustomerId] INT NOT NULL, [UpdatedAt] DATETIME2 (0) NOT NULL, CONSTRAINT [PK_Cart] PRIMARY KEY CLUSTERED ([CustomerId] ASC), CONSTRAINT [FK_Cart_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]) ); GO -- Table [dbo].[CustomerOrder] CREATE TABLE [dbo].[CustomerOrder] ( [CustomerOrderId] INT IDENTITY (1, 1) NOT NULL, [OrderNumber] VARCHAR (20) NOT NULL, [CustomerId] INT NOT NULL, [ShipLine1] NVARCHAR (200) NOT NULL, [ShipLine2] NVARCHAR (200) NULL, [ShipCity] NVARCHAR (100) NOT NULL, [ShipPostalCode] VARCHAR (20) NOT NULL, [ShipCountryCode] CHAR (2) NOT NULL, [PlacedAt] DATETIME2 (0) NOT NULL, [Status] VARCHAR (10) NOT NULL, CONSTRAINT [PK_CustomerOrder] PRIMARY KEY CLUSTERED ([CustomerOrderId] ASC), CONSTRAINT [CK_CustomerOrder_Status] CHECK (Status IN ('Placed', 'Shipped', 'Delivered', 'Cancelled')), CONSTRAINT [FK_CustomerOrder_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]), CONSTRAINT [AK_CustomerOrder_OrderNumber] UNIQUE NONCLUSTERED ([OrderNumber] ASC) ); GO -- Table [dbo].[Product] CREATE TABLE [dbo].[Product] ( [ProductId] INT IDENTITY (1, 1) NOT NULL, [Sku] VARCHAR (20) NOT NULL, [Name] NVARCHAR (200) NOT NULL, [CategoryId] INT NOT NULL, [Price] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ([ProductId] ASC), CONSTRAINT [CK_Product_Price] CHECK (Price >= 0), CONSTRAINT [FK_Product_Category_CategoryId] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Category] ([CategoryId]), CONSTRAINT [AK_Product_Sku] UNIQUE NONCLUSTERED ([Sku] ASC) ); GO -- Table [dbo].[CartItem] CREATE TABLE [dbo].[CartItem] ( [CustomerId] INT NOT NULL, [ProductId] INT NOT NULL, [Quantity] SMALLINT NOT NULL, CONSTRAINT [PK_CartItem] PRIMARY KEY CLUSTERED ([CustomerId] ASC, [ProductId] ASC), CONSTRAINT [CK_CartItem_Quantity] CHECK (Quantity > 0), CONSTRAINT [FK_CartItem_Cart_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Cart] ([CustomerId]), CONSTRAINT [FK_CartItem_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([ProductId]) ); GO -- Table [dbo].[OrderItem] CREATE TABLE [dbo].[OrderItem] ( [CustomerOrderId] INT NOT NULL, [ProductId] INT NOT NULL, [Quantity] SMALLINT NOT NULL, [UnitPrice] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_OrderItem] PRIMARY KEY CLUSTERED ([CustomerOrderId] ASC, [ProductId] ASC), CONSTRAINT [CK_OrderItem_Quantity] CHECK (Quantity > 0), CONSTRAINT [CK_OrderItem_UnitPrice] CHECK (UnitPrice >= 0), CONSTRAINT [FK_OrderItem_CustomerOrder_CustomerOrderId] FOREIGN KEY ([CustomerOrderId]) REFERENCES [dbo].[CustomerOrder] ([CustomerOrderId]), CONSTRAINT [FK_OrderItem_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([ProductId]) ); GO -- Table [dbo].[Payment] CREATE TABLE [dbo].[Payment] ( [PaymentId] INT IDENTITY (1, 1) NOT NULL, [CustomerOrderId] INT NOT NULL, [PaidAt] DATETIME2 (0) NOT NULL, [Amount] DECIMAL (10, 2) NOT NULL, [Method] VARCHAR (10) NOT NULL, CONSTRAINT [PK_Payment] PRIMARY KEY CLUSTERED ([PaymentId] ASC), CONSTRAINT [CK_Payment_Amount] CHECK (Amount > 0), CONSTRAINT [CK_Payment_Method] CHECK (Method IN ('Card', 'PayPal', 'GiftCard')), CONSTRAINT [FK_Payment_CustomerOrder_CustomerOrderId] FOREIGN KEY ([CustomerOrderId]) REFERENCES [dbo].[CustomerOrder] ([CustomerOrderId]) ); GO -- Table [dbo].[Review] CREATE TABLE [dbo].[Review] ( [ProductId] INT NOT NULL, [CustomerId] INT NOT NULL, [Rating] TINYINT NOT NULL, [Body] NVARCHAR (2000) NULL, [ReviewedAt] DATETIME2 (0) NOT NULL, CONSTRAINT [PK_Review] PRIMARY KEY CLUSTERED ([ProductId] ASC, [CustomerId] ASC), CONSTRAINT [CK_Review_Rating] CHECK (Rating BETWEEN 1 AND 5), CONSTRAINT [FK_Review_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]), CONSTRAINT [FK_Review_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([ProductId]) ); GO
Questions
What tables does an online shopping system need?
Customers, products, orders and order items at the least. Addresses, categories, carts, payments and reviews come next. This design has all ten.
Is this the same as an e-commerce website database?
Yes. An e-commerce site's core is these tables: the catalogue, the cart, orders with their lines, and payments. Shipping, coupons and stock levels are tables you add on top; the inventory design covers stock.
Why doesn't the order point to the address table?
An address can be edited or deleted after the order ships. The order copies the address so its record stays as it was.
Can I change the design?
Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.
Does it work with MySQL or PostgreSQL?
The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types and the filtered index need that database's own syntax.
