Hotel management system database: ER diagram and SQL

A hotel management system database in 9 tables: room types and rooms, guests, bookings, the nights each room is taken, services, charges and payments. The ER diagram, what each table holds, and the SQL Server script, ready for a DBMS project or a real app and open to change in ERDsmith.

  • SQL Server
  • 9 tables
  • 8 relationships
ER diagram of the hotel database: dbo.RoomType with dbo.Room, dbo.Guest with dbo.Booking, dbo.BookingRoom between bookings and rooms, dbo.RoomNight, dbo.Service with dbo.Charge, and dbo.Payment, with their keys and relationships.
Drawn by ERDsmith from the script below. Open it to move tables, add columns or pick the IDEF1X look preset.

The tables

The nine tables fall into four groups: the rooms, the guests and their bookings, the nights each room is taken, and the bill.

  • dbo.RoomTypeDouble, suite and the like, each name once, with its capacity and nightly rate.
  • dbo.RoomRooms by number, each of one type, on one floor.
  • dbo.GuestOne row per guest who books.
  • dbo.BookingA guest's stay: when it was booked, the check-in and check-out dates.
  • dbo.BookingRoomThe rooms on a booking, each with the rate agreed for it; its key is the pair.
  • dbo.RoomNightOne row per room per night it is taken, and the booking that holds it.
  • dbo.ServiceBreakfast, laundry and other extras, each name once, with its price.
  • dbo.ChargeAn extra added to a booking: the service, the quantity and the price charged.
  • dbo.PaymentMoney paid against a booking, by cash, card or transfer.

How the tables relate

  • A room type has zero or more rooms , and each room is exactly one type: RoomTypeId is NOT NULL, so the line has nothing at the room type’s end .
  • A guest has zero or more bookings; each booking is one guest’s.
  • Bookings and rooms are many-to-many: dbo.BookingRoom holds one row per pair, keyed by both columns, a composite key. Both its lines are solid and its box has round corners .
  • A room night points at one row of dbo.BookingRoom through the pair (BookingId, RoomId), a foreign key of two columns. The line is dashed because BookingId is not part of the night’s primary key, which makes the relationship non-identifying.
  • A booking has zero or more charges and zero or more payments; a service appears on any number of charges.

Why it is built this way

A room is taken once a night

A stay is a range of dates, and two ranges that overlap are hard to refuse with a constraint. So a booking writes one row per room per night into dbo.RoomNight, keyed by (RoomId, NightOf), and an overlap becomes a duplicate key: room 101 booked a second time for the night of April 11 fails with Msg 2627. The rooms free from check-in to check-out are the ones with no night in that range:

SELECT r.RoomNumber, t.Name, t.NightlyRate
FROM dbo.Room AS r
JOIN dbo.RoomType AS t ON t.RoomTypeId = r.RoomTypeId
WHERE NOT EXISTS (
    SELECT 1 FROM dbo.RoomNight AS n
    WHERE n.RoomId = r.RoomId
      AND n.NightOf >= @CheckInOn
      AND n.NightOf < @CheckOutOn
);

The check-out date itself is not a night, so NightOf stays before it. Nothing in the schema checks that a night falls between its booking’s check-in and check-out; the application writes the nights from those dates.

A night belongs to a room on its booking

The two-column foreign key to dbo.BookingRoom means a night can only be held for a room its booking has. A night for a room that is not on the booking fails with Msg 547, referential integrity on both columns at once.

Prices are copied when they are agreed

dbo.BookingRoom keeps the nightly rate agreed for that booking, and dbo.Charge the unit price charged, so a new rate on dbo.RoomType or dbo.Service never rewrites an old bill. The checks hold the rest: a check-out date that is not after the check-in fails with Msg 547, and so does a payment method other than cash, card or transfer.

The SQL Server script

hotel.sql · 128 linesDownload
-- Table [dbo].[Guest]
CREATE TABLE [dbo].[Guest] (
    [GuestId]  INT            IDENTITY (1, 1) NOT NULL,
    [FullName] NVARCHAR (200) NOT NULL,
    [Email]    NVARCHAR (254) NULL,
    [Phone]    VARCHAR (20)   NULL,
    CONSTRAINT [PK_Guest] PRIMARY KEY CLUSTERED ([GuestId] ASC)
);


GO

-- Table [dbo].[RoomType]
CREATE TABLE [dbo].[RoomType] (
    [RoomTypeId]  INT            IDENTITY (1, 1) NOT NULL,
    [Name]        NVARCHAR (50)  NOT NULL,
    [Capacity]    TINYINT        NOT NULL,
    [NightlyRate] DECIMAL (8, 2) NOT NULL,
    CONSTRAINT [PK_RoomType] PRIMARY KEY CLUSTERED ([RoomTypeId] ASC),
    CONSTRAINT [CK_RoomType_Capacity] CHECK (Capacity > 0),
    CONSTRAINT [CK_RoomType_NightlyRate] CHECK (NightlyRate > 0),
    CONSTRAINT [AK_RoomType_Name] UNIQUE NONCLUSTERED ([Name] ASC)
);


GO

-- Table [dbo].[Service]
CREATE TABLE [dbo].[Service] (
    [ServiceId] INT            IDENTITY (1, 1) NOT NULL,
    [Name]      NVARCHAR (100) NOT NULL,
    [Price]     DECIMAL (8, 2) NOT NULL,
    CONSTRAINT [PK_Service] PRIMARY KEY CLUSTERED ([ServiceId] ASC),
    CONSTRAINT [CK_Service_Price] CHECK (Price >= 0),
    CONSTRAINT [AK_Service_Name] UNIQUE NONCLUSTERED ([Name] ASC)
);


GO

-- Table [dbo].[Booking]
CREATE TABLE [dbo].[Booking] (
    [BookingId]  INT           IDENTITY (1, 1) NOT NULL,
    [GuestId]    INT           NOT NULL,
    [BookedAt]   DATETIME2 (0) NOT NULL,
    [CheckInOn]  DATE          NOT NULL,
    [CheckOutOn] DATE          NOT NULL,
    CONSTRAINT [PK_Booking] PRIMARY KEY CLUSTERED ([BookingId] ASC),
    CONSTRAINT [CK_Booking_CheckOutOn] CHECK (CheckOutOn > CheckInOn),
    CONSTRAINT [FK_Booking_Guest_GuestId] FOREIGN KEY ([GuestId]) REFERENCES [dbo].[Guest] ([GuestId])
);


GO

-- Table [dbo].[Room]
CREATE TABLE [dbo].[Room] (
    [RoomId]     INT          IDENTITY (1, 1) NOT NULL,
    [RoomNumber] VARCHAR (10) NOT NULL,
    [RoomTypeId] INT          NOT NULL,
    [Floor]      SMALLINT     NOT NULL,
    CONSTRAINT [PK_Room] PRIMARY KEY CLUSTERED ([RoomId] ASC),
    CONSTRAINT [FK_Room_RoomType_RoomTypeId] FOREIGN KEY ([RoomTypeId]) REFERENCES [dbo].[RoomType] ([RoomTypeId]),
    CONSTRAINT [AK_Room_RoomNumber] UNIQUE NONCLUSTERED ([RoomNumber] ASC)
);


GO

-- Table [dbo].[BookingRoom]
CREATE TABLE [dbo].[BookingRoom] (
    [BookingId]   INT            NOT NULL,
    [RoomId]      INT            NOT NULL,
    [NightlyRate] DECIMAL (8, 2) NOT NULL,
    CONSTRAINT [PK_BookingRoom] PRIMARY KEY CLUSTERED ([BookingId] ASC, [RoomId] ASC),
    CONSTRAINT [CK_BookingRoom_NightlyRate] CHECK (NightlyRate > 0),
    CONSTRAINT [FK_BookingRoom_Booking_BookingId] FOREIGN KEY ([BookingId]) REFERENCES [dbo].[Booking] ([BookingId]),
    CONSTRAINT [FK_BookingRoom_Room_RoomId] FOREIGN KEY ([RoomId]) REFERENCES [dbo].[Room] ([RoomId])
);


GO

-- Table [dbo].[Charge]
CREATE TABLE [dbo].[Charge] (
    [ChargeId]  INT            IDENTITY (1, 1) NOT NULL,
    [BookingId] INT            NOT NULL,
    [ServiceId] INT            NOT NULL,
    [ChargedAt] DATETIME2 (0)  NOT NULL,
    [Quantity]  SMALLINT       NOT NULL,
    [UnitPrice] DECIMAL (8, 2) NOT NULL,
    CONSTRAINT [PK_Charge] PRIMARY KEY CLUSTERED ([ChargeId] ASC),
    CONSTRAINT [CK_Charge_Quantity] CHECK (Quantity > 0),
    CONSTRAINT [CK_Charge_UnitPrice] CHECK (UnitPrice >= 0),
    CONSTRAINT [FK_Charge_Booking_BookingId] FOREIGN KEY ([BookingId]) REFERENCES [dbo].[Booking] ([BookingId]),
    CONSTRAINT [FK_Charge_Service_ServiceId] FOREIGN KEY ([ServiceId]) REFERENCES [dbo].[Service] ([ServiceId])
);


GO

-- Table [dbo].[Payment]
CREATE TABLE [dbo].[Payment] (
    [PaymentId] INT             IDENTITY (1, 1) NOT NULL,
    [BookingId] 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', 'Transfer')),
    CONSTRAINT [FK_Payment_Booking_BookingId] FOREIGN KEY ([BookingId]) REFERENCES [dbo].[Booking] ([BookingId])
);


GO

-- Table [dbo].[RoomNight]
CREATE TABLE [dbo].[RoomNight] (
    [RoomId]    INT  NOT NULL,
    [NightOf]   DATE NOT NULL,
    [BookingId] INT  NOT NULL,
    CONSTRAINT [PK_RoomNight] PRIMARY KEY CLUSTERED ([RoomId] ASC, [NightOf] ASC),
    CONSTRAINT [FK_RoomNight_BookingRoom_BookingId_RoomId] FOREIGN KEY ([BookingId], [RoomId]) REFERENCES [dbo].[BookingRoom] ([BookingId], [RoomId])
);


GO

Questions

What tables does a hotel management system need?

Rooms, guests and bookings at the least. Room types, the rooms on each booking, the nights each room is taken, services, charges and payments come next. This design has all nine.

How do I find the free rooms for some dates?

Select the rooms with no row in dbo.RoomNight from the check-in date up to the night before check-out. The full query is under "Why it is built this way".

What does a guest owe?

The nights at each room's agreed rate, plus the charges, minus the payments: three sums over dbo.RoomNight joined to dbo.BookingRoom, dbo.Charge and dbo.Payment for that booking.

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 IDENTITY columns need that database's own syntax.

More templates and reading