The tables
The design’s eight tables fall into three groups: the catalogue, the stock and the buying.
dbo.CategoryProduct categories by name, each name once.dbo.ProductOne row per item, found by SKU: its unit and the level that calls for a reorder.dbo.WarehouseThe places stock is kept, found by a short code.dbo.StockHow many of a product a warehouse holds now; its key is the pair.dbo.StockMovementEvery change to a stock level: when, how many in or out, and why.dbo.SupplierThe companies stock is bought from, each name once.dbo.PurchaseOrderAn order to a supplier for one warehouse: ordered and received dates.dbo.PurchaseOrderLineA product on a purchase order, with quantity and unit cost; its key is the pair.
How the tables relate
- Warehouses and products are many-to-many:
dbo.Stockholds one row per pair, keyed by both columns, a composite key. Its lines are solid and its box has rounded corners . - A stock row has zero or more movements , and each movement belongs to exactly one stock row, through a foreign key of two columns,
(WarehouseId, ProductId). Both are NOT NULL, so the line has nothing at the stock row’s end . - A supplier has zero or more purchase orders; each purchase order is from exactly one supplier and delivers to exactly one warehouse.
- Purchase orders and products are many-to-many through
dbo.PurchaseOrderLine, keyed by both columns. - A movement has zero or one purchase order:
PurchaseOrderIdallows NULL and is set only on a receipt, so the line has a ring alone at the purchase order’s end .
Why it is built this way
Stock can’t go below zero
CK_Stock_Quantity checks Quantity >= 0. Issuing 60 boxes from a warehouse that holds 50 fails with Msg 547, so the application can’t sell what isn’t on the shelf. The primary key (WarehouseId, ProductId) keeps one stock row per product per warehouse; a second row fails with Msg 2627.
Every change is a movement
dbo.Stock holds the level now; dbo.StockMovement holds how it got there: a receipt adds, an issue takes away (a negative quantity), a transfer moves stock between warehouses and a count corrects the level after a stocktake. The application writes the movement and updates the stock row in one transaction; nothing in the schema adds them up for it.
A movement needs a stock row
The movement’s foreign key is two columns, (WarehouseId, ProductId), pointing at dbo.Stock’s primary key, not one key to the warehouse and another to the product. A movement for a product the warehouse doesn’t stock fails with Msg 547: the warehouse starts stocking it first, with a stock row at 0. Read more on how SQL Server keeps that rule in referential integrity.
Only receipts name a purchase order
CK_StockMovement_PurchaseOrderId allows a purchase order on a movement only when Reason is Receipt; an issue that names one fails with Msg 547. A purchase order holds each product once: a second line for the same product fails with Msg 2627.
Reorder from the stock levels
Each product carries its ReorderLevel. The products at or below it, per warehouse:
SELECT w.Code, p.Sku, p.Name, s.Quantity, p.ReorderLevel FROM dbo.Stock AS s JOIN dbo.Product AS p ON p.ProductId = s.ProductId JOIN dbo.Warehouse AS w ON w.WarehouseId = s.WarehouseId WHERE s.Quantity <= p.ReorderLevel ORDER BY w.Code, p.Sku;
The SQL Server script
-- Table [dbo].[Category] CREATE TABLE [dbo].[Category] ( [CategoryId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED ([CategoryId] ASC), CONSTRAINT [AK_Category_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Supplier] CREATE TABLE [dbo].[Supplier] ( [SupplierId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (200) NOT NULL, [Email] NVARCHAR (254) NULL, [Phone] VARCHAR (20) NULL, CONSTRAINT [PK_Supplier] PRIMARY KEY CLUSTERED ([SupplierId] ASC), CONSTRAINT [AK_Supplier_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Warehouse] CREATE TABLE [dbo].[Warehouse] ( [WarehouseId] INT IDENTITY (1, 1) NOT NULL, [Code] VARCHAR (10) NOT NULL, [Name] NVARCHAR (100) NOT NULL, [City] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_Warehouse] PRIMARY KEY CLUSTERED ([WarehouseId] ASC), CONSTRAINT [AK_Warehouse_Code] UNIQUE NONCLUSTERED ([Code] 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, [Unit] VARCHAR (10) NOT NULL, [ReorderLevel] INT NOT NULL, CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ([ProductId] ASC), CONSTRAINT [CK_Product_ReorderLevel] CHECK (ReorderLevel >= 0), CONSTRAINT [FK_Product_Category_CategoryId] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Category] ([CategoryId]), CONSTRAINT [AK_Product_Sku] UNIQUE NONCLUSTERED ([Sku] ASC) ); GO -- Table [dbo].[PurchaseOrder] CREATE TABLE [dbo].[PurchaseOrder] ( [PurchaseOrderId] INT IDENTITY (1, 1) NOT NULL, [OrderNumber] VARCHAR (20) NOT NULL, [SupplierId] INT NOT NULL, [WarehouseId] INT NOT NULL, [OrderedOn] DATE NOT NULL, [ReceivedOn] DATE NULL, CONSTRAINT [PK_PurchaseOrder] PRIMARY KEY CLUSTERED ([PurchaseOrderId] ASC), CONSTRAINT [CK_PurchaseOrder_ReceivedOn] CHECK (ReceivedOn >= OrderedOn), CONSTRAINT [FK_PurchaseOrder_Supplier_SupplierId] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([SupplierId]), CONSTRAINT [FK_PurchaseOrder_Warehouse_WarehouseId] FOREIGN KEY ([WarehouseId]) REFERENCES [dbo].[Warehouse] ([WarehouseId]), CONSTRAINT [AK_PurchaseOrder_OrderNumber] UNIQUE NONCLUSTERED ([OrderNumber] ASC) ); GO -- Table [dbo].[PurchaseOrderLine] CREATE TABLE [dbo].[PurchaseOrderLine] ( [PurchaseOrderId] INT NOT NULL, [ProductId] INT NOT NULL, [Quantity] INT NOT NULL, [UnitCost] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_PurchaseOrderLine] PRIMARY KEY CLUSTERED ([PurchaseOrderId] ASC, [ProductId] ASC), CONSTRAINT [CK_PurchaseOrderLine_Quantity] CHECK (Quantity > 0), CONSTRAINT [CK_PurchaseOrderLine_UnitCost] CHECK (UnitCost >= 0), CONSTRAINT [FK_PurchaseOrderLine_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([ProductId]), CONSTRAINT [FK_PurchaseOrderLine_PurchaseOrder_PurchaseOrderId] FOREIGN KEY ([PurchaseOrderId]) REFERENCES [dbo].[PurchaseOrder] ([PurchaseOrderId]) ); GO -- Table [dbo].[Stock] CREATE TABLE [dbo].[Stock] ( [WarehouseId] INT NOT NULL, [ProductId] INT NOT NULL, [Quantity] INT NOT NULL, CONSTRAINT [PK_Stock] PRIMARY KEY CLUSTERED ([WarehouseId] ASC, [ProductId] ASC), CONSTRAINT [CK_Stock_Quantity] CHECK (Quantity >= 0), CONSTRAINT [FK_Stock_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([ProductId]), CONSTRAINT [FK_Stock_Warehouse_WarehouseId] FOREIGN KEY ([WarehouseId]) REFERENCES [dbo].[Warehouse] ([WarehouseId]) ); GO -- Table [dbo].[StockMovement] CREATE TABLE [dbo].[StockMovement] ( [StockMovementId] INT IDENTITY (1, 1) NOT NULL, [WarehouseId] INT NOT NULL, [ProductId] INT NOT NULL, [MovedAt] DATETIME2 (0) NOT NULL, [Quantity] INT NOT NULL, [Reason] VARCHAR (10) NOT NULL, [PurchaseOrderId] INT NULL, CONSTRAINT [PK_StockMovement] PRIMARY KEY CLUSTERED ([StockMovementId] ASC), CONSTRAINT [CK_StockMovement_PurchaseOrderId] CHECK (PurchaseOrderId IS NULL OR Reason = 'Receipt'), CONSTRAINT [CK_StockMovement_Quantity] CHECK (Quantity <> 0), CONSTRAINT [CK_StockMovement_Reason] CHECK (Reason IN ('Receipt', 'Issue', 'Transfer', 'Count')), CONSTRAINT [FK_StockMovement_PurchaseOrder_PurchaseOrderId] FOREIGN KEY ([PurchaseOrderId]) REFERENCES [dbo].[PurchaseOrder] ([PurchaseOrderId]), CONSTRAINT [FK_StockMovement_Stock_WarehouseId_ProductId] FOREIGN KEY ([WarehouseId], [ProductId]) REFERENCES [dbo].[Stock] ([WarehouseId], [ProductId]) ); GO
Questions
What tables does an inventory management system need?
Products, warehouses and stock levels at the least. Suppliers, purchase orders and their lines, stock movements and categories come next. This design has all eight.
Why keep both a stock level and the movements?
The level answers "how many now" in one row. The movements answer "where did they go" and let you rebuild the level: the sum of a product's movements in a warehouse.
How do I find what needs reordering?
Join the stock to its product and keep the rows where Quantity is at or below ReorderLevel. The full query is under "Why it is built this way".
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, checks and relationships carry over to other databases, the two-column foreign key included; the types need that database's own names.
