The tables
Nine tables in four groups: the floor, the menu, the staff and the service.
dbo.DiningTableThe restaurant's tables by number, with their seats.dbo.ReservationA guest's booking of a table at a start time, with the party size.dbo.MenuCategoryMenu sections by name: starters, mains, desserts.dbo.MenuItemOne dish or drink, each name once, with its price and whether it is available.dbo.StaffWaiters, chefs, cashiers and managers.dbo.TableOrderWhat a table ordered from the moment it was seated: opened, closed and the waiter.dbo.OrderItemA dish on an order, how many and at what price; its key is the pair.dbo.BillAt most one bill per order, its time and total.dbo.PaymentMoney paid against a bill, by cash or card.
How the tables relate
- A dining table has zero or more reservations and orders over time; each of those names exactly one dining table: its
DiningTableIdis NOT NULL, so the line has nothing at the dining table’s end . - Orders and menu items are many-to-many:
dbo.OrderItemholds one row per pair, keyed by both columns, a composite key. - An order has zero or one bill : the bill’s primary key is the order’s key, so the line is solid .
- A bill has zero or more payments, so a table that splits the bill pays in several rows.
- An order is taken by exactly one member of staff; a menu item is in exactly one category.
Why it is built this way
A table has one open order
A filtered unique index on dbo.TableOrder (DiningTableId) WHERE ClosedAt IS NULL lets a table have many past orders and one open order. A second open order on the same table fails with Msg 2601; once the first is closed, the next guests get a new one.
A reservation is checked by start time
The alternate key (DiningTableId, StartsAt) on dbo.Reservation refuses a second booking of the same table at the same time with Msg 2627. It compares start times, not overlapping times: a booking at 19:00 and another at 19:30 on the same table both go in. Checking that two sittings don’t overlap needs a length per booking and a check in the application or a trigger.
A payment needs a bill
dbo.Payment references dbo.Bill, not dbo.TableOrder, so money can’t be taken for an order nobody has billed: a payment on an order with no bill fails with Msg 547. A second bill for the same order fails with Msg 2627, because the bill’s primary key is the order’s own key. What is still due on each bill:
SELECT d.TableNumber, b.Total, b.Total - COALESCE(SUM(p.Amount), 0) AS Due FROM dbo.Bill AS b JOIN dbo.TableOrder AS o ON o.TableOrderId = b.TableOrderId JOIN dbo.DiningTable AS d ON d.DiningTableId = o.DiningTableId LEFT JOIN dbo.Payment AS p ON p.TableOrderId = b.TableOrderId GROUP BY b.TableOrderId, d.TableNumber, b.Total HAVING COALESCE(SUM(p.Amount), 0) < b.Total;
The order keeps its prices
dbo.OrderItem.UnitPrice copies the menu price when the dish is ordered, so a new menu price never changes a bill already printed. dbo.Bill.Total is stored as printed; the application works it out from the order’s items.
TableOrder and DiningTable, not Order and Table
ORDER is a reserved word in SQL Server, so the order table is dbo.TableOrder: no brackets needed in any query. The restaurant’s tables are dbo.DiningTable for the same reason, TABLE being reserved too.
The SQL Server script
-- Table [dbo].[DiningTable] CREATE TABLE [dbo].[DiningTable] ( [DiningTableId] INT IDENTITY (1, 1) NOT NULL, [TableNumber] VARCHAR (5) NOT NULL, [Seats] TINYINT NOT NULL, CONSTRAINT [PK_DiningTable] PRIMARY KEY CLUSTERED ([DiningTableId] ASC), CONSTRAINT [CK_DiningTable_Seats] CHECK (Seats > 0), CONSTRAINT [AK_DiningTable_TableNumber] UNIQUE NONCLUSTERED ([TableNumber] ASC) ); GO -- Table [dbo].[MenuCategory] CREATE TABLE [dbo].[MenuCategory] ( [MenuCategoryId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (50) NOT NULL, CONSTRAINT [PK_MenuCategory] PRIMARY KEY CLUSTERED ([MenuCategoryId] ASC), CONSTRAINT [AK_MenuCategory_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Staff] CREATE TABLE [dbo].[Staff] ( [StaffId] INT IDENTITY (1, 1) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [Role] VARCHAR (10) NOT NULL, CONSTRAINT [PK_Staff] PRIMARY KEY CLUSTERED ([StaffId] ASC), CONSTRAINT [CK_Staff_Role] CHECK (Role IN ('Waiter', 'Chef', 'Cashier', 'Manager')) ); GO -- Table [dbo].[MenuItem] CREATE TABLE [dbo].[MenuItem] ( [MenuItemId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (100) NOT NULL, [MenuCategoryId] INT NOT NULL, [Price] DECIMAL (8, 2) NOT NULL, [IsAvailable] BIT NOT NULL, CONSTRAINT [PK_MenuItem] PRIMARY KEY CLUSTERED ([MenuItemId] ASC), CONSTRAINT [CK_MenuItem_Price] CHECK (Price >= 0), CONSTRAINT [FK_MenuItem_MenuCategory_MenuCategoryId] FOREIGN KEY ([MenuCategoryId]) REFERENCES [dbo].[MenuCategory] ([MenuCategoryId]), CONSTRAINT [AK_MenuItem_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Reservation] CREATE TABLE [dbo].[Reservation] ( [ReservationId] INT IDENTITY (1, 1) NOT NULL, [DiningTableId] INT NOT NULL, [GuestName] NVARCHAR (200) NOT NULL, [Phone] VARCHAR (20) NOT NULL, [PartySize] TINYINT NOT NULL, [StartsAt] DATETIME2 (0) NOT NULL, CONSTRAINT [PK_Reservation] PRIMARY KEY CLUSTERED ([ReservationId] ASC), CONSTRAINT [CK_Reservation_PartySize] CHECK (PartySize > 0), CONSTRAINT [FK_Reservation_DiningTable_DiningTableId] FOREIGN KEY ([DiningTableId]) REFERENCES [dbo].[DiningTable] ([DiningTableId]), CONSTRAINT [AK_Reservation_DiningTableId_StartsAt] UNIQUE NONCLUSTERED ([DiningTableId] ASC, [StartsAt] ASC) ); GO -- Table [dbo].[TableOrder] CREATE TABLE [dbo].[TableOrder] ( [TableOrderId] INT IDENTITY (1, 1) NOT NULL, [DiningTableId] INT NOT NULL, [StaffId] INT NOT NULL, [OpenedAt] DATETIME2 (0) NOT NULL, [ClosedAt] DATETIME2 (0) NULL, CONSTRAINT [PK_TableOrder] PRIMARY KEY CLUSTERED ([TableOrderId] ASC), CONSTRAINT [CK_TableOrder_ClosedAt] CHECK (ClosedAt >= OpenedAt), CONSTRAINT [FK_TableOrder_DiningTable_DiningTableId] FOREIGN KEY ([DiningTableId]) REFERENCES [dbo].[DiningTable] ([DiningTableId]), CONSTRAINT [FK_TableOrder_Staff_StaffId] FOREIGN KEY ([StaffId]) REFERENCES [dbo].[Staff] ([StaffId]) ); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_TableOrder_DiningTableId_Open] ON [dbo].[TableOrder]([DiningTableId] ASC) WHERE (ClosedAt IS NULL); GO -- Table [dbo].[Bill] CREATE TABLE [dbo].[Bill] ( [TableOrderId] INT NOT NULL, [IssuedAt] DATETIME2 (0) NOT NULL, [Total] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_Bill] PRIMARY KEY CLUSTERED ([TableOrderId] ASC), CONSTRAINT [CK_Bill_Total] CHECK (Total >= 0), CONSTRAINT [FK_Bill_TableOrder_TableOrderId] FOREIGN KEY ([TableOrderId]) REFERENCES [dbo].[TableOrder] ([TableOrderId]) ); GO -- Table [dbo].[OrderItem] CREATE TABLE [dbo].[OrderItem] ( [TableOrderId] INT NOT NULL, [MenuItemId] INT NOT NULL, [Quantity] TINYINT NOT NULL, [UnitPrice] DECIMAL (8, 2) NOT NULL, CONSTRAINT [PK_OrderItem] PRIMARY KEY CLUSTERED ([TableOrderId] ASC, [MenuItemId] ASC), CONSTRAINT [CK_OrderItem_Quantity] CHECK (Quantity > 0), CONSTRAINT [CK_OrderItem_UnitPrice] CHECK (UnitPrice >= 0), CONSTRAINT [FK_OrderItem_MenuItem_MenuItemId] FOREIGN KEY ([MenuItemId]) REFERENCES [dbo].[MenuItem] ([MenuItemId]), CONSTRAINT [FK_OrderItem_TableOrder_TableOrderId] FOREIGN KEY ([TableOrderId]) REFERENCES [dbo].[TableOrder] ([TableOrderId]) ); GO -- Table [dbo].[Payment] CREATE TABLE [dbo].[Payment] ( [PaymentId] INT IDENTITY (1, 1) NOT NULL, [TableOrderId] 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 ('Cash', 'Card')), CONSTRAINT [FK_Payment_Bill_TableOrderId] FOREIGN KEY ([TableOrderId]) REFERENCES [dbo].[Bill] ([TableOrderId]) ); GO
Questions
What tables does a restaurant management system need?
Dining tables, menu items, orders and their items at the least. Menu categories, staff, reservations, bills and payments come next. This design has all nine.
How do I split a bill?
Add one payment row per guest against the same bill. The query under "Why it is built this way" shows what is still due.
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.
