The tables
Eight tables in four groups: the network, the fleet, the flights and the sales.
dbo.AirportOne row per airport, found by its three-letter IATA code.dbo.RouteAn origin and a destination airport with the distance between them; each pair once.dbo.AircraftOne row per plane, found by its registration.dbo.SeatThe seats of a plane and their cabin; a seat number once per aircraft.dbo.FlightA flight number on a route at a departure time, flown by one aircraft.dbo.PassengerThe person who flies: name, birth date, passport number when known.dbo.BookingWhat was bought together, found by its six-character reference.dbo.TicketOne passenger on one flight in a booking, with the fare and, once given, a seat.
How the tables relate
- A route has zero or more flights , and each flight flies exactly one route with exactly one aircraft: both foreign keys are NOT NULL, so their lines have nothing at the route’s or the aircraft’s end .
dbo.Routehas two foreign keys todbo.Airport,OriginAirportIdandDestinationAirportId, so the diagram draws two lines between them: an airport starts some routes and ends others.- An aircraft has zero or more seats, and a seat number is unique within its aircraft, so a plane has one seat 32C.
- Flights and passengers are many-to-many, and
dbo.Ticketis the table between them. It keeps its ownTicketIdand carries the fare, the booking and the seat. - A booking holds the tickets bought together: a return trip for two people is four tickets under one reference.
- A ticket’s seat is zero or one:
SeatIdallows NULL until a seat is given, so the line has a ring alone at the seat’s end .
Why it is built this way
A route never ends where it starts
CK_Route_DestinationAirportId checks that the destination differs from the origin, so a route from Seattle to Seattle fails with Msg 547. The alternate key on the pair of airports keeps each route once; the way back is a route of its own.
A flight number repeats, a departure doesn’t
A flight number such as BA48 flies on many days, so the number alone can’t be a key. The pair (FlightNumber, DepartsAt) is the alternate key, and a second BA48 at the same departure fails with Msg 2627. DepartsAt and ArrivesAt are datetimeoffset, each in its own airport’s offset, and CK_Flight_ArrivesAt compares them as instants.
A seat is sold once per flight
The alternate key (FlightId, PassengerId) refuses a second ticket for the same passenger on the same flight with Msg 2627. Seats are often given later, so many tickets have no seat yet. A unique constraint counts NULL as a value and would let only one seatless ticket in (shown in types of keys in DBMS). The filtered unique index UX_Ticket_FlightId_SeatId_Assigned covers only rows WHERE SeatId IS NOT NULL: any number of tickets wait for a seat, and seat 32C given to a second passenger on the same flight fails with Msg 2601.
The seat’s aircraft is the application’s check
Nothing in the schema checks that a ticket’s seat is on the aircraft flying that flight: SeatId references any row of dbo.Seat. The application gives seats from the flight’s own aircraft, as this query of the free seats on a flight does:
SELECT s.SeatNumber, s.Cabin FROM dbo.Flight AS f JOIN dbo.Seat AS s ON s.AircraftId = f.AircraftId WHERE f.FlightId = @FlightId AND NOT EXISTS ( SELECT 1 FROM dbo.Ticket AS t WHERE t.FlightId = f.FlightId AND t.SeatId = s.SeatId );
Passengers have no natural key
Names repeat and a passport number isn’t always known when the booking is made, so dbo.Passenger has only its surrogate key, PassengerId. The booking’s reference and the ticket number are the codes people read out over the phone, and each is an alternate key.
The SQL Server script
-- Table [dbo].[Aircraft] CREATE TABLE [dbo].[Aircraft] ( [AircraftId] INT IDENTITY (1, 1) NOT NULL, [Registration] VARCHAR (10) NOT NULL, [Model] VARCHAR (50) NOT NULL, CONSTRAINT [PK_Aircraft] PRIMARY KEY CLUSTERED ([AircraftId] ASC), CONSTRAINT [AK_Aircraft_Registration] UNIQUE NONCLUSTERED ([Registration] ASC) ); GO -- Table [dbo].[Airport] CREATE TABLE [dbo].[Airport] ( [AirportId] INT IDENTITY (1, 1) NOT NULL, [IataCode] CHAR (3) NOT NULL, [Name] NVARCHAR (100) NOT NULL, [City] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_Airport] PRIMARY KEY CLUSTERED ([AirportId] ASC), CONSTRAINT [AK_Airport_IataCode] UNIQUE NONCLUSTERED ([IataCode] ASC) ); GO -- Table [dbo].[Booking] CREATE TABLE [dbo].[Booking] ( [BookingId] INT IDENTITY (1, 1) NOT NULL, [Reference] CHAR (6) NOT NULL, [BookedAt] DATETIME2 (0) NOT NULL, [ContactEmail] NVARCHAR (254) NOT NULL, CONSTRAINT [PK_Booking] PRIMARY KEY CLUSTERED ([BookingId] ASC), CONSTRAINT [AK_Booking_Reference] UNIQUE NONCLUSTERED ([Reference] ASC) ); GO -- Table [dbo].[Passenger] CREATE TABLE [dbo].[Passenger] ( [PassengerId] INT IDENTITY (1, 1) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [BirthDate] DATE NOT NULL, [PassportNumber] VARCHAR (20) NULL, [Email] NVARCHAR (254) NULL, CONSTRAINT [PK_Passenger] PRIMARY KEY CLUSTERED ([PassengerId] ASC) ); GO -- Table [dbo].[Route] CREATE TABLE [dbo].[Route] ( [RouteId] INT IDENTITY (1, 1) NOT NULL, [OriginAirportId] INT NOT NULL, [DestinationAirportId] INT NOT NULL, [DistanceKm] SMALLINT NOT NULL, CONSTRAINT [PK_Route] PRIMARY KEY CLUSTERED ([RouteId] ASC), CONSTRAINT [CK_Route_DestinationAirportId] CHECK (DestinationAirportId <> OriginAirportId), CONSTRAINT [CK_Route_DistanceKm] CHECK (DistanceKm > 0), CONSTRAINT [FK_Route_Airport_DestinationAirportId] FOREIGN KEY ([DestinationAirportId]) REFERENCES [dbo].[Airport] ([AirportId]), CONSTRAINT [FK_Route_Airport_OriginAirportId] FOREIGN KEY ([OriginAirportId]) REFERENCES [dbo].[Airport] ([AirportId]), CONSTRAINT [AK_Route_OriginAirportId_DestinationAirportId] UNIQUE NONCLUSTERED ([OriginAirportId] ASC, [DestinationAirportId] ASC) ); GO -- Table [dbo].[Seat] CREATE TABLE [dbo].[Seat] ( [SeatId] INT IDENTITY (1, 1) NOT NULL, [AircraftId] INT NOT NULL, [SeatNumber] VARCHAR (4) NOT NULL, [Cabin] VARCHAR (10) NOT NULL, CONSTRAINT [PK_Seat] PRIMARY KEY CLUSTERED ([SeatId] ASC), CONSTRAINT [CK_Seat_Cabin] CHECK (Cabin IN ('Economy', 'Business', 'First')), CONSTRAINT [FK_Seat_Aircraft_AircraftId] FOREIGN KEY ([AircraftId]) REFERENCES [dbo].[Aircraft] ([AircraftId]), CONSTRAINT [AK_Seat_AircraftId_SeatNumber] UNIQUE NONCLUSTERED ([AircraftId] ASC, [SeatNumber] ASC) ); GO -- Table [dbo].[Flight] CREATE TABLE [dbo].[Flight] ( [FlightId] INT IDENTITY (1, 1) NOT NULL, [FlightNumber] VARCHAR (8) NOT NULL, [RouteId] INT NOT NULL, [AircraftId] INT NOT NULL, [DepartsAt] DATETIMEOFFSET (0) NOT NULL, [ArrivesAt] DATETIMEOFFSET (0) NOT NULL, CONSTRAINT [PK_Flight] PRIMARY KEY CLUSTERED ([FlightId] ASC), CONSTRAINT [CK_Flight_ArrivesAt] CHECK (ArrivesAt > DepartsAt), CONSTRAINT [FK_Flight_Aircraft_AircraftId] FOREIGN KEY ([AircraftId]) REFERENCES [dbo].[Aircraft] ([AircraftId]), CONSTRAINT [FK_Flight_Route_RouteId] FOREIGN KEY ([RouteId]) REFERENCES [dbo].[Route] ([RouteId]), CONSTRAINT [AK_Flight_FlightNumber_DepartsAt] UNIQUE NONCLUSTERED ([FlightNumber] ASC, [DepartsAt] ASC) ); GO -- Table [dbo].[Ticket] CREATE TABLE [dbo].[Ticket] ( [TicketId] INT IDENTITY (1, 1) NOT NULL, [TicketNumber] CHAR (13) NOT NULL, [BookingId] INT NOT NULL, [FlightId] INT NOT NULL, [PassengerId] INT NOT NULL, [SeatId] INT NULL, [Fare] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_Ticket] PRIMARY KEY CLUSTERED ([TicketId] ASC), CONSTRAINT [CK_Ticket_Fare] CHECK (Fare >= 0), CONSTRAINT [FK_Ticket_Booking_BookingId] FOREIGN KEY ([BookingId]) REFERENCES [dbo].[Booking] ([BookingId]), CONSTRAINT [FK_Ticket_Flight_FlightId] FOREIGN KEY ([FlightId]) REFERENCES [dbo].[Flight] ([FlightId]), CONSTRAINT [FK_Ticket_Passenger_PassengerId] FOREIGN KEY ([PassengerId]) REFERENCES [dbo].[Passenger] ([PassengerId]), CONSTRAINT [FK_Ticket_Seat_SeatId] FOREIGN KEY ([SeatId]) REFERENCES [dbo].[Seat] ([SeatId]), CONSTRAINT [AK_Ticket_FlightId_PassengerId] UNIQUE NONCLUSTERED ([FlightId] ASC, [PassengerId] ASC), CONSTRAINT [AK_Ticket_TicketNumber] UNIQUE NONCLUSTERED ([TicketNumber] ASC) ); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_Ticket_FlightId_SeatId_Assigned] ON [dbo].[Ticket]([FlightId] ASC, [SeatId] ASC) WHERE (SeatId IS NOT NULL); GO
Questions
What tables does an airline reservation system need?
Airports, flights, passengers, bookings and tickets at the least. Routes, aircraft and seats come next. This design has all eight.
How do I find the free seats on a flight?
Select the seats of the flight's aircraft that no ticket on that flight holds: NOT EXISTS on dbo.Ticket for that flight and seat. The full query is under "Why it is built this way".
Why is a ticket separate from a booking?
A booking is one purchase, one reference and one contact email; a ticket is one passenger on one flight. A family on a return trip is one booking and many tickets.
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; the types and the filtered index need that database's own syntax. Both allow repeated NULLs in a unique key, so there a plain UNIQUE (FlightId, SeatId) does the filtered index's job.
