Car Rental ER diagram and database schema

The Car Rental database schema is a DB Planner template with 23 tables, 174 columns and 45 relationships, free to use. Core tables include additional_drivers, coverage_products, customers, damage_photos. It can be opened as an interactive ER diagram or exported as PostgreSQL, MySQL, or SQL Server DDL.

ER diagram of the Car Rental database schema, showing 23 tables and their relationships
FIG. 1: entity relationship diagram, 23 tables
Tables
23
Columns
174
Relationships
45
Category
Marketplace
Exports
DBML, SQL, Prisma, C#
Price
Free

What this schema models

A complete car rental system database design, built the way rental companies actually operate: 23 tables, 11 enums and 45 relationships covering locations and staff, vehicle classes and the fleet, maintenance, damage and transfers, rate plans, extras and protection products, customers and driver's licenses, reservations, rental agreements and itemized billing. THE FOUR DESIGN RULES 1. Reserve a CLASS, rent a CAR. A reservation promises a vehicle class ("Toyota Corolla or similar"); the specific car is assigned only at pickup, on the rental. Availability is counted per class, per location, per time window. 2. A reservation is not a rental. The reservation is the promise and its quoted price; the rental agreement is what actually happened: odometer and fuel out and in, real times, the license that was checked. Walk-up rentals simply have no reservation. 3. Prices are snapshots. The quote is frozen on the reservation and every final charge is its own line, so editing a rate plan never changes a confirmed booking or an issued invoice. 4. Card activity is rows, not a balance. The deposit authorization at pickup, the capture at return, releases and refunds are separate payments, each pointing at its parent. DETAILS WORTH STEALING - Vehicle classes use ACRISS codes, the industry standard (ECMR, CDAR and so on). - vehicles.status is a cached convenience; the authoritative "is it on rent?" is an open rental, enforced by a partial unique index. - Damage reports with photos are recorded at checkout too, so a customer is never billed for damage that was already there. - Fuel is recorded in eighths, as on a real checkout sheet, so refuelling charges are computable. - One-way rentals and fleet rebalancing are both vehicle transfers. - Cards are stored only as payment processor tokens. WHAT YOU GET Clean DBML that imports directly into DB Planner, and a laid-out board with zones color-coded and framed: locations and staff, fleet, pricing and products, reservations, rentals, billing and customers. Every table carries a note. Postgres-flavoured types that port cleanly to MySQL or SQL Server. Useful for developers building a rental or fleet booking platform, and for students who need a realistic car rental system ER diagram for a DBMS project. This is a schema design reference; tax and insurance rules vary by market and belong in your own business logic.

Tables (23)

Every table and its columns

locations

Branches where cars are picked up and returned.

Columns of the locations table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(10)unique, not nulle.g. an airport code plus a suffix, "DFW01"
namevarchar(200)not null
address_line1varchar(200)not null
cityvarchar(120)not null
countrychar(2)not null
timezonevarchar(64)not nullIANA zone, e.g. America/Chicago. Pickup and return times are shown in it.
is_airportbooleannot null, default false

location_hours

Counter opening hours in the location timezone. Pickups outside them need after-hours approval.

Columns of the location_hours table
ColumnTypeConstraintsReferencesDescription
location_iduuidFK, not nulllocations.id
day_of_weeksmallintnot null1 = Monday ... 7 = Sunday
opens_attimenot null
closes_attimenot null

staff

Counter agents, lot staff and managers. Every handover records who did it.

Columns of the staff table
ColumnTypeConstraintsReferences
iduuidPK
location_iduuidFK, not nulllocations.id
namevarchar(200)not null
emailvarchar(254)unique, not null
rolestaff_rolenot null

vehicle_classes

What customers book. A reservation promises a CLASS; the specific car is chosen at pickup.

Columns of the vehicle_classes table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codechar(4)unique, not nullACRISS code, e.g. ECMR = economy, 2-4 door, manual, air conditioning
namevarchar(100)not nulle.g. Economy, Midsize SUV
seatssmallintnot null
doorssmallintnot null
transmissiontransmissionnot null
fuelfuel_typenot null
large_bagssmallintnot null

vehicle_models

Make and model, grouped into a class. "Toyota Corolla or similar" is this relationship.

Columns of the vehicle_models table
ColumnTypeConstraintsReferences
iduuidPK
vehicle_class_iduuidFK, not nullvehicle_classes.id
makevarchar(60)not null
modelvarchar(60)not null
model_yearsmallintnot null

vehicles

One physical car. status is a cached convenience; the authoritative answer to "is it on rent?" is an open row in rentals.

Columns of the vehicles table
ColumnTypeConstraintsReferencesDescription
iduuidPK
vinchar(17)unique, not null
platevarchar(15)not null
vehicle_model_iduuidFK, not nullvehicle_models.id
colorvarchar(30)
home_location_iduuidFK, not nulllocations.id
current_location_iduuidFKlocations.idNull while on rent or in transit
statusvehicle_statusnot null, default 'available'
odometer_kmintegernot null
in_fleet_ondatenot null
out_of_fleet_ondate

maintenance_records

Service, repairs and recalls. An open record keeps the car off the rentable list.

Columns of the maintenance_records table
ColumnTypeConstraintsReferences
iduuidPK
vehicle_iduuidFK, not nullvehicles.id
kindmaintenance_kindnot null
opened_attimestamptznot null, default `now()`
closed_attimestamptz
odometer_kmintegernot null
cost_centsinteger
notestext

damage_reports

Damage found at check-in or inspection. Existing damage is recorded at checkout so a customer is never billed for it.

Columns of the damage_reports table
ColumnTypeConstraintsReferencesDescription
iduuidPK
vehicle_iduuidFK, not nullvehicles.id
rental_iduuidFKrentals.idNull for damage found outside a rental, e.g. in the lot
reported_byuuidFK, not nullstaff.id
reported_attimestamptznot null, default `now()`
severitydamage_severitynot null
descriptiontextnot null
repaired_attimestamptz

damage_photos

Evidence for a damage report.

Columns of the damage_photos table
ColumnTypeConstraintsReferencesDescription
iduuidPK
damage_report_iduuidFK, not nulldamage_reports.id
storage_keyvarchar(300)not nullObject storage key. Images never live in the database.
taken_attimestamptznot null

vehicle_transfers

Fleet rebalancing between locations, and one-way rentals coming home.

Columns of the vehicle_transfers table
ColumnTypeConstraintsReferences
iduuidPK
vehicle_iduuidFK, not nullvehicles.id
from_location_iduuidFK, not nulllocations.id
to_location_iduuidFK, not nulllocations.id
departed_attimestamptznot null
arrived_attimestamptz
driver_staff_iduuidFKstaff.id

rate_plans

Price lists by class and pickup location. Reservations snapshot the quote, so editing a plan never changes a confirmed price.

Columns of the rate_plans table
ColumnTypeConstraintsReferencesDescription
iduuidPK
vehicle_class_iduuidFK, not nullvehicle_classes.id
location_iduuidFK, not nulllocations.id
namevarchar(100)not nulle.g. Standard, Weekend, Prepaid
currencychar(3)not null
daily_rate_centsintegernot null
weekly_rate_centsintegerApplied from 5 or more days, when set
included_km_per_dayintegerNull for unlimited mileage
extra_km_centsinteger
valid_fromdatenot null
valid_todate
prepaidbooleannot null, default false

extras

Add-ons sold with a rental.

Columns of the extras table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(20)unique, not nulle.g. GPS, CHILD_SEAT, ADDL_DRIVER, TOLL_PASS
namevarchar(100)not null
price_centsintegernot null
pricingextra_pricingnot null
max_per_rentalsmallintnot null, default 1

coverage_products

Protection products: collision damage waiver, liability, personal accident.

Columns of the coverage_products table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(20)unique, not nulle.g. CDW, SLI, PAI
namevarchar(100)not null
daily_price_centsintegernot null
deductible_centsintegerWhat the customer still owes on a claim; null for none

customers

People who book or drive. An additional driver is a customer too.

Columns of the customers table
ColumnTypeConstraintsReferencesDescription
iduuidPK
emailvarchar(254)unique, not null
first_namevarchar(100)not null
last_namevarchar(100)not null
phonevarchar(30)
date_of_birthdatenot nullDrives young-driver surcharges and minimum-age rules
loyalty_numbervarchar(20)unique
do_not_rentbooleannot null, default false
created_attimestamptznot null, default `now()`

driver_licenses

Licenses on file. A rental points at the exact license checked at the counter.

Columns of the driver_licenses table
ColumnTypeConstraintsReferencesDescription
iduuidPK
customer_iduuidFK, not nullcustomers.id
license_numbervarchar(40)not null
issuing_countrychar(2)not null
issuing_regionvarchar(60)State or province, where it applies
expires_ondatenot null
verified_attimestamptz
verified_byuuidFKstaff.id

payment_methods

Saved cards, as processor tokens.

Columns of the payment_methods table
ColumnTypeConstraintsReferencesDescription
iduuidPK
customer_iduuidFK, not nullcustomers.id
processor_tokenvarchar(200)not nullCard data stays with the payment processor; only its token is stored
brandvarchar(20)not null
last4char(4)not null
expires_ondatenot null

reservations

A promise of a vehicle class, at a place and time, for a quoted price. Never a specific car.

Columns of the reservations table
ColumnTypeConstraintsReferencesDescription
iduuidPK
confirmation_codevarchar(12)unique, not null
customer_iduuidFK, not nullcustomers.id
vehicle_class_iduuidFK, not nullvehicle_classes.id
rate_plan_iduuidFK, not nullrate_plans.id
pickup_location_iduuidFK, not nulllocations.id
return_location_iduuidFK, not nulllocations.idDifferent from pickup for a one-way rental
pickup_attimestamptznot null
return_attimestamptznot null
quoted_total_centsintegernot nullSnapshot of the price shown at booking
currencychar(3)not null
statusreservation_statusnot null, default 'confirmed'
created_attimestamptznot null, default `now()`
cancelled_attimestamptz

Indexes: (pickup_location_id, vehicle_class_id, pickup_at)

reservation_extras

Add-ons requested with a booking.

Columns of the reservation_extras table
ColumnTypeConstraintsReferencesDescription
reservation_iduuidFK, not nullreservations.id
extra_iduuidFK, not nullextras.id
quantitysmallintnot null, default 1
price_centsintegernot nullPrice at booking

reservation_coverages

Protection products chosen at booking. Can still be added or declined at the counter.

Columns of the reservation_coverages table
ColumnTypeConstraintsReferencesDescription
reservation_iduuidFK, not nullreservations.id
coverage_product_iduuidFK, not nullcoverage_products.id
daily_price_centsintegernot nullPrice at booking

rentals

The rental agreement. Odometer and fuel at both ends are what mileage and refuelling charges are computed from.

Columns of the rentals table
ColumnTypeConstraintsReferencesDescription
iduuidPK
agreement_numbervarchar(20)unique, not null
reservation_iduuidFK, uniquereservations.idNull for walk-up rentals
customer_iduuidFK, not nullcustomers.id
driver_license_iduuidFK, not nulldriver_licenses.id
vehicle_iduuidFK, not nullvehicles.idThe specific car, assigned at pickup
pickup_location_iduuidFK, not nulllocations.id
picked_up_attimestamptznot null
odometer_out_kmintegernot null
fuel_out_eighthssmallintnot nullFuel level 0-8, as on the checkout sheet
checked_out_byuuidFK, not nullstaff.id
expected_return_attimestamptznot null
return_location_iduuidFKlocations.id
returned_attimestamptz
odometer_in_kminteger
fuel_in_eighthssmallint
checked_in_byuuidFKstaff.id
statusrental_statusnot null, default 'open'

Indexes: (vehicle_id, status)

additional_drivers

Everyone else authorized to drive on an agreement, with the license that was checked.

Columns of the additional_drivers table
ColumnTypeConstraintsReferences
rental_iduuidFK, not nullrentals.id
customer_iduuidFK, not nullcustomers.id
driver_license_iduuidFK, not nulldriver_licenses.id

rental_charges

Every line on the final invoice: time, extras, coverage, mileage, fuel, late return, one-way fee, damage, taxes. The invoice total is their sum.

Columns of the rental_charges table
ColumnTypeConstraintsReferencesDescription
iduuidPK
rental_iduuidFK, not nullrentals.id
kindcharge_kindnot null
descriptionvarchar(200)not nulle.g. "5 days x $42.00", "Refuelling, 3/8 tank"
quantitynumeric(8,2)not null, default 1
unit_price_centsintegernot null
amount_centsintegernot nullNegative for discounts and credits
created_attimestamptznot null, default `now()`

payments

Card activity as separate rows: the deposit authorization at pickup, the capture at return, any release or refund.

Columns of the payments table
ColumnTypeConstraintsReferencesDescription
iduuidPK
rental_iduuidFKrentals.id
reservation_iduuidFKreservations.idSet for prepaid bookings charged before a rental exists
payment_method_iduuidFK, not nullpayment_methods.id
kindpayment_kindnot null
amount_centsintegernot null
currencychar(3)not null
processor_referencevarchar(100)not null
parent_payment_iduuidFKpayments.idA capture or release points at its authorization; a refund at its capture
created_attimestamptznot null, default `now()`

Enums (11)

staff_role
counter_agent, lot_attendant, mechanic, location_manager
transmission
automatic, manual
fuel_type
petrol, diesel, hybrid, electric
vehicle_status
available, reserved, on_rent, in_transit, maintenance, out_of_service, sold
maintenance_kind
scheduled_service, repair, tires, recall, cleaning
damage_severity
cosmetic, minor, major, total_loss
extra_pricing
per_day, per_rental
reservation_status
confirmed, cancelled, no_show, fulfilled
rental_status
open, closed, disputed
charge_kind
time, extra, coverage, mileage, fuel, late_return, one_way_fee, young_driver, damage, tax, discount
payment_kind
authorization, capture, release, refund

Relationships (45)

  • additional_drivers.customer_id references customers.id many to one
  • additional_drivers.driver_license_id references driver_licenses.id many to one
  • additional_drivers.rental_id references rentals.id many to one
  • damage_photos.damage_report_id references damage_reports.id many to one
  • damage_reports.rental_id references rentals.id many to one
  • damage_reports.reported_by references staff.id many to one
  • damage_reports.vehicle_id references vehicles.id many to one
  • driver_licenses.customer_id references customers.id many to one
  • driver_licenses.verified_by references staff.id many to one
  • location_hours.location_id references locations.id many to one
  • maintenance_records.vehicle_id references vehicles.id many to one
  • payment_methods.customer_id references customers.id many to one
  • payments.parent_payment_id references payments.id many to one
  • payments.payment_method_id references payment_methods.id many to one
  • payments.rental_id references rentals.id many to one
  • payments.reservation_id references reservations.id many to one
  • rate_plans.location_id references locations.id many to one
  • rate_plans.vehicle_class_id references vehicle_classes.id many to one
  • rental_charges.rental_id references rentals.id many to one
  • rentals.checked_in_by references staff.id many to one
  • rentals.checked_out_by references staff.id many to one
  • rentals.customer_id references customers.id many to one
  • rentals.driver_license_id references driver_licenses.id many to one
  • rentals.pickup_location_id references locations.id many to one
  • rentals.return_location_id references locations.id many to one
  • rentals.vehicle_id references vehicles.id many to one
  • reservation_coverages.coverage_product_id references coverage_products.id many to one
  • reservation_coverages.reservation_id references reservations.id many to one
  • reservation_extras.extra_id references extras.id many to one
  • reservation_extras.reservation_id references reservations.id many to one
  • reservations.customer_id references customers.id many to one
  • reservations.id references rentals.reservation_id one to one
  • reservations.pickup_location_id references locations.id many to one
  • reservations.rate_plan_id references rate_plans.id many to one
  • reservations.return_location_id references locations.id many to one
  • reservations.vehicle_class_id references vehicle_classes.id many to one
  • staff.location_id references locations.id many to one
  • vehicle_models.vehicle_class_id references vehicle_classes.id many to one
  • vehicle_transfers.driver_staff_id references staff.id many to one
  • vehicle_transfers.from_location_id references locations.id many to one
  • vehicle_transfers.to_location_id references locations.id many to one
  • vehicle_transfers.vehicle_id references vehicles.id many to one
  • vehicles.current_location_id references locations.id many to one
  • vehicles.home_location_id references locations.id many to one
  • vehicles.vehicle_model_id references vehicle_models.id many to one

Car Rental database schema in DBML

The schema as it is written: DBML, the text format DB Planner draws its diagrams from. Copy it into any DBML tool, or open it in the diagram above.

car-rental.dbml dbml
// Car Rental
// Zones (header colors): locations and staff (gray), fleet (teal),
// pricing and products (blue), customers (green), reservations (amber),
// rentals (coral), billing (purple).

// ---------------------------------------------------------------------------
// LOCATIONS AND STAFF
// ---------------------------------------------------------------------------

Table locations [headercolor: #90a4ae] {
  id uuid [pk]
  code varchar(10) [not null, unique, note: 'e.g. an airport code plus a suffix, "DFW01"']
  name varchar(200) [not null]
  address_line1 varchar(200) [not null]
  city varchar(120) [not null]
  country char(2) [not null]
  timezone varchar(64) [not null, note: 'IANA zone, e.g. America/Chicago. Pickup and return times are shown in it.']
  is_airport boolean [not null, default: false]
  Note: 'Branches where cars are picked up and returned.'
}

Table location_hours [headercolor: #90a4ae] {
  location_id uuid [not null, ref: > locations.id]
  day_of_week smallint [not null, note: '1 = Monday ... 7 = Sunday']
  opens_at time [not null]
  closes_at time [not null]

  indexes {
    (location_id, day_of_week) [pk]
  }

  Note: 'Counter opening hours in the location timezone. Pickups outside them need after-hours approval.'
}

Table staff [headercolor: #90a4ae] {
  id uuid [pk]
  location_id uuid [not null, ref: > locations.id]
  name varchar(200) [not null]
  email varchar(254) [not null, unique]
  role staff_role [not null]
  Note: 'Counter agents, lot staff and managers. Every handover records who did it.'
}

// ---------------------------------------------------------------------------
// FLEET: the cars themselves
// ---------------------------------------------------------------------------

Table vehicle_classes [headercolor: #4cd0b0] {
  id uuid [pk]
  code char(4) [not null, unique, note: 'ACRISS code, e.g. ECMR = economy, 2-4 door, manual, air conditioning']
  name varchar(100) [not null, note: 'e.g. Economy, Midsize SUV']
  seats smallint [not null]
  doors smallint [not null]
  transmission transmission [not null]
  fuel fuel_type [not null]
  large_bags smallint [not null]
  Note: 'What customers book. A reservation promises a CLASS; the specific car is chosen at pickup.'
}

Table vehicle_models [headercolor: #4cd0b0] {
  id uuid [pk]
  vehicle_class_id uuid [not null, ref: > vehicle_classes.id]
  make varchar(60) [not null]
  model varchar(60) [not null]
  model_year smallint [not null]
  Note: 'Make and model, grouped into a class. "Toyota Corolla or similar" is this relationship.'
}

Table vehicles [headercolor: #4cd0b0] {
  id uuid [pk]
  vin char(17) [not null, unique]
  plate varchar(15) [not null]
  vehicle_model_id uuid [not null, ref: > vehicle_models.id]
  color varchar(30)
  home_location_id uuid [not null, ref: > locations.id]
  current_location_id uuid [ref: > locations.id, note: 'Null while on rent or in transit']
  status vehicle_status [not null, default: 'available']
  odometer_km integer [not null]
  in_fleet_on date [not null]
  out_of_fleet_on date
  Note: 'One physical car. status is a cached convenience; the authoritative answer to "is it on rent?" is an open row in rentals.'
}

Table maintenance_records [headercolor: #4cd0b0] {
  id uuid [pk]
  vehicle_id uuid [not null, ref: > vehicles.id]
  kind maintenance_kind [not null]
  opened_at timestamptz [not null, default: `now()`]
  closed_at timestamptz
  odometer_km integer [not null]
  cost_cents integer
  notes text
  Note: 'Service, repairs and recalls. An open record keeps the car off the rentable list.'
}

Table damage_reports [headercolor: #4cd0b0] {
  id uuid [pk]
  vehicle_id uuid [not null, ref: > vehicles.id]
  rental_id uuid [ref: > rentals.id, note: 'Null for damage found outside a rental, e.g. in the lot']
  reported_by uuid [not null, ref: > staff.id]
  reported_at timestamptz [not null, default: `now()`]
  severity damage_severity [not null]
  description text [not null]
  repaired_at timestamptz
  Note: 'Damage found at check-in or inspection. Existing damage is recorded at checkout so a customer is never billed for it.'
}

Table damage_photos [headercolor: #4cd0b0] {
  id uuid [pk]
  damage_report_id uuid [not null, ref: > damage_reports.id]
  storage_key varchar(300) [not null, note: 'Object storage key. Images never live in the database.']
  taken_at timestamptz [not null]
  Note: 'Evidence for a damage report.'
}

Table vehicle_transfers [headercolor: #4cd0b0] {
  id uuid [pk]
  vehicle_id uuid [not null, ref: > vehicles.id]
  from_location_id uuid [not null, ref: > locations.id]
  to_location_id uuid [not null, ref: > locations.id]
  departed_at timestamptz [not null]
  arrived_at timestamptz
  driver_staff_id uuid [ref: > staff.id]
  Note: 'Fleet rebalancing between locations, and one-way rentals coming home.'
}

// ---------------------------------------------------------------------------
// PRICING AND PRODUCTS
// ---------------------------------------------------------------------------

Table rate_plans [headercolor: #4c8bf5] {
  id uuid [pk]
  vehicle_class_id uuid [not null, ref: > vehicle_classes.id]
  location_id uuid [not null, ref: > locations.id]
  name varchar(100) [not null, note: 'e.g. Standard, Weekend, Prepaid']
  currency char(3) [not null]
  daily_rate_cents integer [not null]
  weekly_rate_cents integer [note: 'Applied from 5 or more days, when set']
  included_km_per_day integer [note: 'Null for unlimited mileage']
  extra_km_cents integer
  valid_from date [not null]
  valid_to date
  prepaid boolean [not null, default: false]
  Note: 'Price lists by class and pickup location. Reservations snapshot the quote, so editing a plan never changes a confirmed price.'
}

Table extras [headercolor: #4c8bf5] {
  id uuid [pk]
  code varchar(20) [not null, unique, note: 'e.g. GPS, CHILD_SEAT, ADDL_DRIVER, TOLL_PASS']
  name varchar(100) [not null]
  price_cents integer [not null]
  pricing extra_pricing [not null]
  max_per_rental smallint [not null, default: 1]
  Note: 'Add-ons sold with a rental.'
}

Table coverage_products [headercolor: #4c8bf5] {
  id uuid [pk]
  code varchar(20) [not null, unique, note: 'e.g. CDW, SLI, PAI']
  name varchar(100) [not null]
  daily_price_cents integer [not null]
  deductible_cents integer [note: 'What the customer still owes on a claim; null for none']
  Note: 'Protection products: collision damage waiver, liability, personal accident.'
}

// ---------------------------------------------------------------------------
// CUSTOMERS
// ---------------------------------------------------------------------------

Table customers [headercolor: #8bc34a] {
  id uuid [pk]
  email varchar(254) [not null, unique]
  first_name varchar(100) [not null]
  last_name varchar(100) [not null]
  phone varchar(30)
  date_of_birth date [not null, note: 'Drives young-driver surcharges and minimum-age rules']
  loyalty_number varchar(20) [unique]
  do_not_rent boolean [not null, default: false]
  created_at timestamptz [not null, default: `now()`]
  Note: 'People who book or drive. An additional driver is a customer too.'
}

Table driver_licenses [headercolor: #8bc34a] {
  id uuid [pk]
  customer_id uuid [not null, ref: > customers.id]
  license_number varchar(40) [not null]
  issuing_country char(2) [not null]
  issuing_region varchar(60) [note: 'State or province, where it applies']
  expires_on date [not null]
  verified_at timestamptz
  verified_by uuid [ref: > staff.id]
  Note: 'Licenses on file. A rental points at the exact license checked at the counter.'
}

Table payment_methods [headercolor: #8bc34a] {
  id uuid [pk]
  customer_id uuid [not null, ref: > customers.id]
  processor_token varchar(200) [not null, note: 'Card data stays with the payment processor; only its token is stored']
  brand varchar(20) [not null]
  last4 char(4) [not null]
  expires_on date [not null]
  Note: 'Saved cards, as processor tokens.'
}

// ---------------------------------------------------------------------------
// RESERVATIONS: what was promised
// ---------------------------------------------------------------------------

Table reservations [headercolor: #f5b74c] {
  id uuid [pk]
  confirmation_code varchar(12) [not null, unique]
  customer_id uuid [not null, ref: > customers.id]
  vehicle_class_id uuid [not null, ref: > vehicle_classes.id]
  rate_plan_id uuid [not null, ref: > rate_plans.id]
  pickup_location_id uuid [not null, ref: > locations.id]
  return_location_id uuid [not null, ref: > locations.id, note: 'Different from pickup for a one-way rental']
  pickup_at timestamptz [not null]
  return_at timestamptz [not null]
  quoted_total_cents integer [not null, note: 'Snapshot of the price shown at booking']
  currency char(3) [not null]
  status reservation_status [not null, default: 'confirmed']
  created_at timestamptz [not null, default: `now()`]
  cancelled_at timestamptz

  indexes {
    (pickup_location_id, vehicle_class_id, pickup_at) [note: 'Availability: bookings per class per location per time window']
  }

  Note: 'A promise of a vehicle class, at a place and time, for a quoted price. Never a specific car.'
}

Table reservation_extras [headercolor: #f5b74c] {
  reservation_id uuid [not null, ref: > reservations.id]
  extra_id uuid [not null, ref: > extras.id]
  quantity smallint [not null, default: 1]
  price_cents integer [not null, note: 'Price at booking']

  indexes {
    (reservation_id, extra_id) [pk]
  }

  Note: 'Add-ons requested with a booking.'
}

Table reservation_coverages [headercolor: #f5b74c] {
  reservation_id uuid [not null, ref: > reservations.id]
  coverage_product_id uuid [not null, ref: > coverage_products.id]
  daily_price_cents integer [not null, note: 'Price at booking']

  indexes {
    (reservation_id, coverage_product_id) [pk]
  }

  Note: 'Protection products chosen at booking. Can still be added or declined at the counter.'
}

// ---------------------------------------------------------------------------
// RENTALS: what actually happened
// ---------------------------------------------------------------------------

Table rentals [headercolor: #f07178] {
  id uuid [pk]
  agreement_number varchar(20) [not null, unique]
  reservation_id uuid [unique, ref: - reservations.id, note: 'Null for walk-up rentals']
  customer_id uuid [not null, ref: > customers.id]
  driver_license_id uuid [not null, ref: > driver_licenses.id]
  vehicle_id uuid [not null, ref: > vehicles.id, note: 'The specific car, assigned at pickup']
  pickup_location_id uuid [not null, ref: > locations.id]
  picked_up_at timestamptz [not null]
  odometer_out_km integer [not null]
  fuel_out_eighths smallint [not null, note: 'Fuel level 0-8, as on the checkout sheet']
  checked_out_by uuid [not null, ref: > staff.id]
  expected_return_at timestamptz [not null]
  return_location_id uuid [ref: > locations.id]
  returned_at timestamptz
  odometer_in_km integer
  fuel_in_eighths smallint
  checked_in_by uuid [ref: > staff.id]
  status rental_status [not null, default: 'open']

  indexes {
    (vehicle_id, status) [note: 'Enforce one open rental per vehicle with a partial unique index WHERE status = open']
  }

  Note: 'The rental agreement. Odometer and fuel at both ends are what mileage and refuelling charges are computed from.'
}

Table additional_drivers [headercolor: #f07178] {
  rental_id uuid [not null, ref: > rentals.id]
  customer_id uuid [not null, ref: > customers.id]
  driver_license_id uuid [not null, ref: > driver_licenses.id]

  indexes {
    (rental_id, customer_id) [pk]
  }

  Note: 'Everyone else authorized to drive on an agreement, with the license that was checked.'
}

// ---------------------------------------------------------------------------
// BILLING: itemized, then settled
// ---------------------------------------------------------------------------

Table rental_charges [headercolor: #b98cf0] {
  id uuid [pk]
  rental_id uuid [not null, ref: > rentals.id]
  kind charge_kind [not null]
  description varchar(200) [not null, note: 'e.g. "5 days x $42.00", "Refuelling, 3/8 tank"']
  quantity numeric(8,2) [not null, default: 1]
  unit_price_cents integer [not null]
  amount_cents integer [not null, note: 'Negative for discounts and credits']
  created_at timestamptz [not null, default: `now()`]
  Note: 'Every line on the final invoice: time, extras, coverage, mileage, fuel, late return, one-way fee, damage, taxes. The invoice total is their sum.'
}

Table payments [headercolor: #b98cf0] {
  id uuid [pk]
  rental_id uuid [ref: > rentals.id]
  reservation_id uuid [ref: > reservations.id, note: 'Set for prepaid bookings charged before a rental exists']
  payment_method_id uuid [not null, ref: > payment_methods.id]
  kind payment_kind [not null]
  amount_cents integer [not null]
  currency char(3) [not null]
  processor_reference varchar(100) [not null]
  parent_payment_id uuid [ref: > payments.id, note: 'A capture or release points at its authorization; a refund at its capture']
  created_at timestamptz [not null, default: `now()`]
  Note: 'Card activity as separate rows: the deposit authorization at pickup, the capture at return, any release or refund.'
}

// ---------------------------------------------------------------------------
// ENUMS
// ---------------------------------------------------------------------------

Enum staff_role {
  counter_agent
  lot_attendant
  mechanic
  location_manager
}

Enum transmission {
  automatic
  manual
}

Enum fuel_type {
  petrol
  diesel
  hybrid
  electric
}

Enum vehicle_status {
  available
  reserved
  on_rent
  in_transit
  maintenance
  out_of_service
  sold
}

Enum maintenance_kind {
  scheduled_service
  repair
  tires
  recall
  cleaning
}

Enum damage_severity {
  cosmetic
  minor
  major
  total_loss
}

Enum extra_pricing {
  per_day
  per_rental
}

Enum reservation_status {
  confirmed
  cancelled
  no_show
  fulfilled
}

Enum rental_status {
  open
  closed
  disputed
}

Enum charge_kind {
  time
  extra
  coverage
  mileage
  fuel
  late_return
  one_way_fee
  young_driver
  damage
  tax
  discount
}

Enum payment_kind {
  authorization
  capture
  release
  refund
}

Car Rental database schema as SQL

The same schema as CREATE TABLE statements, exported by DB Planner. PostgreSQL is shown first; MySQL and SQL Server are below it.

PostgreSQL
postgresql.sql PostgreSQL
CREATE TYPE "staff_role" AS ENUM (
  'counter_agent',
  'lot_attendant',
  'mechanic',
  'location_manager'
);

CREATE TYPE "transmission" AS ENUM (
  'automatic',
  'manual'
);

CREATE TYPE "fuel_type" AS ENUM (
  'petrol',
  'diesel',
  'hybrid',
  'electric'
);

CREATE TYPE "vehicle_status" AS ENUM (
  'available',
  'reserved',
  'on_rent',
  'in_transit',
  'maintenance',
  'out_of_service',
  'sold'
);

CREATE TYPE "maintenance_kind" AS ENUM (
  'scheduled_service',
  'repair',
  'tires',
  'recall',
  'cleaning'
);

CREATE TYPE "damage_severity" AS ENUM (
  'cosmetic',
  'minor',
  'major',
  'total_loss'
);

CREATE TYPE "extra_pricing" AS ENUM (
  'per_day',
  'per_rental'
);

CREATE TYPE "reservation_status" AS ENUM (
  'confirmed',
  'cancelled',
  'no_show',
  'fulfilled'
);

CREATE TYPE "rental_status" AS ENUM (
  'open',
  'closed',
  'disputed'
);

CREATE TYPE "charge_kind" AS ENUM (
  'time',
  'extra',
  'coverage',
  'mileage',
  'fuel',
  'late_return',
  'one_way_fee',
  'young_driver',
  'damage',
  'tax',
  'discount'
);

CREATE TYPE "payment_kind" AS ENUM (
  'authorization',
  'capture',
  'release',
  'refund'
);

CREATE TABLE "locations" (
  "id" uuid PRIMARY KEY,
  "code" varchar(10) UNIQUE NOT NULL,
  "name" varchar(200) NOT NULL,
  "address_line1" varchar(200) NOT NULL,
  "city" varchar(120) NOT NULL,
  "country" char(2) NOT NULL,
  "timezone" varchar(64) NOT NULL,
  "is_airport" boolean NOT NULL DEFAULT false
);

CREATE TABLE "location_hours" (
  "location_id" uuid NOT NULL,
  "day_of_week" smallint NOT NULL,
  "opens_at" time NOT NULL,
  "closes_at" time NOT NULL,
  PRIMARY KEY ("location_id", "day_of_week")
);

CREATE TABLE "staff" (
  "id" uuid PRIMARY KEY,
  "location_id" uuid NOT NULL,
  "name" varchar(200) NOT NULL,
  "email" varchar(254) UNIQUE NOT NULL,
  "role" staff_role NOT NULL
);

CREATE TABLE "vehicle_classes" (
  "id" uuid PRIMARY KEY,
  "code" char(4) UNIQUE NOT NULL,
  "name" varchar(100) NOT NULL,
  "seats" smallint NOT NULL,
  "doors" smallint NOT NULL,
  "transmission" transmission NOT NULL,
  "fuel" fuel_type NOT NULL,
  "large_bags" smallint NOT NULL
);

CREATE TABLE "vehicle_models" (
  "id" uuid PRIMARY KEY,
  "vehicle_class_id" uuid NOT NULL,
  "make" varchar(60) NOT NULL,
  "model" varchar(60) NOT NULL,
  "model_year" smallint NOT NULL
);

CREATE TABLE "vehicles" (
  "id" uuid PRIMARY KEY,
  "vin" char(17) UNIQUE NOT NULL,
  "plate" varchar(15) NOT NULL,
  "vehicle_model_id" uuid NOT NULL,
  "color" varchar(30),
  "home_location_id" uuid NOT NULL,
  "current_location_id" uuid,
  "status" vehicle_status NOT NULL DEFAULT 'available',
  "odometer_km" integer NOT NULL,
  "in_fleet_on" date NOT NULL,
  "out_of_fleet_on" date
);

CREATE TABLE "maintenance_records" (
  "id" uuid PRIMARY KEY,
  "vehicle_id" uuid NOT NULL,
  "kind" maintenance_kind NOT NULL,
  "opened_at" timestamptz NOT NULL DEFAULT (now()),
  "closed_at" timestamptz,
  "odometer_km" integer NOT NULL,
  "cost_cents" integer,
  "notes" text
);

CREATE TABLE "damage_reports" (
  "id" uuid PRIMARY KEY,
  "vehicle_id" uuid NOT NULL,
  "rental_id" uuid,
  "reported_by" uuid NOT NULL,
  "reported_at" timestamptz NOT NULL DEFAULT (now()),
  "severity" damage_severity NOT NULL,
  "description" text NOT NULL,
  "repaired_at" timestamptz
);

CREATE TABLE "damage_photos" (
  "id" uuid PRIMARY KEY,
  "damage_report_id" uuid NOT NULL,
  "storage_key" varchar(300) NOT NULL,
  "taken_at" timestamptz NOT NULL
);

CREATE TABLE "vehicle_transfers" (
  "id" uuid PRIMARY KEY,
  "vehicle_id" uuid NOT NULL,
  "from_location_id" uuid NOT NULL,
  "to_location_id" uuid NOT NULL,
  "departed_at" timestamptz NOT NULL,
  "arrived_at" timestamptz,
  "driver_staff_id" uuid
);

CREATE TABLE "rate_plans" (
  "id" uuid PRIMARY KEY,
  "vehicle_class_id" uuid NOT NULL,
  "location_id" uuid NOT NULL,
  "name" varchar(100) NOT NULL,
  "currency" char(3) NOT NULL,
  "daily_rate_cents" integer NOT NULL,
  "weekly_rate_cents" integer,
  "included_km_per_day" integer,
  "extra_km_cents" integer,
  "valid_from" date NOT NULL,
  "valid_to" date,
  "prepaid" boolean NOT NULL DEFAULT false
);

CREATE TABLE "extras" (
  "id" uuid PRIMARY KEY,
  "code" varchar(20) UNIQUE NOT NULL,
  "name" varchar(100) NOT NULL,
  "price_cents" integer NOT NULL,
  "pricing" extra_pricing NOT NULL,
  "max_per_rental" smallint NOT NULL DEFAULT 1
);

CREATE TABLE "coverage_products" (
  "id" uuid PRIMARY KEY,
  "code" varchar(20) UNIQUE NOT NULL,
  "name" varchar(100) NOT NULL,
  "daily_price_cents" integer NOT NULL,
  "deductible_cents" integer
);

CREATE TABLE "customers" (
  "id" uuid PRIMARY KEY,
  "email" varchar(254) UNIQUE NOT NULL,
  "first_name" varchar(100) NOT NULL,
  "last_name" varchar(100) NOT NULL,
  "phone" varchar(30),
  "date_of_birth" date NOT NULL,
  "loyalty_number" varchar(20) UNIQUE,
  "do_not_rent" boolean NOT NULL DEFAULT false,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "driver_licenses" (
  "id" uuid PRIMARY KEY,
  "customer_id" uuid NOT NULL,
  "license_number" varchar(40) NOT NULL,
  "issuing_country" char(2) NOT NULL,
  "issuing_region" varchar(60),
  "expires_on" date NOT NULL,
  "verified_at" timestamptz,
  "verified_by" uuid
);

CREATE TABLE "payment_methods" (
  "id" uuid PRIMARY KEY,
  "customer_id" uuid NOT NULL,
  "processor_token" varchar(200) NOT NULL,
  "brand" varchar(20) NOT NULL,
  "last4" char(4) NOT NULL,
  "expires_on" date NOT NULL
);

CREATE TABLE "reservations" (
  "id" uuid PRIMARY KEY,
  "confirmation_code" varchar(12) UNIQUE NOT NULL,
  "customer_id" uuid NOT NULL,
  "vehicle_class_id" uuid NOT NULL,
  "rate_plan_id" uuid NOT NULL,
  "pickup_location_id" uuid NOT NULL,
  "return_location_id" uuid NOT NULL,
  "pickup_at" timestamptz NOT NULL,
  "return_at" timestamptz NOT NULL,
  "quoted_total_cents" integer NOT NULL,
  "currency" char(3) NOT NULL,
  "status" reservation_status NOT NULL DEFAULT 'confirmed',
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "cancelled_at" timestamptz
);

CREATE TABLE "reservation_extras" (
  "reservation_id" uuid NOT NULL,
  "extra_id" uuid NOT NULL,
  "quantity" smallint NOT NULL DEFAULT 1,
  "price_cents" integer NOT NULL,
  PRIMARY KEY ("reservation_id", "extra_id")
);

CREATE TABLE "reservation_coverages" (
  "reservation_id" uuid NOT NULL,
  "coverage_product_id" uuid NOT NULL,
  "daily_price_cents" integer NOT NULL,
  PRIMARY KEY ("reservation_id", "coverage_product_id")
);

CREATE TABLE "rentals" (
  "id" uuid PRIMARY KEY,
  "agreement_number" varchar(20) UNIQUE NOT NULL,
  "reservation_id" uuid UNIQUE,
  "customer_id" uuid NOT NULL,
  "driver_license_id" uuid NOT NULL,
  "vehicle_id" uuid NOT NULL,
  "pickup_location_id" uuid NOT NULL,
  "picked_up_at" timestamptz NOT NULL,
  "odometer_out_km" integer NOT NULL,
  "fuel_out_eighths" smallint NOT NULL,
  "checked_out_by" uuid NOT NULL,
  "expected_return_at" timestamptz NOT NULL,
  "return_location_id" uuid,
  "returned_at" timestamptz,
  "odometer_in_km" integer,
  "fuel_in_eighths" smallint,
  "checked_in_by" uuid,
  "status" rental_status NOT NULL DEFAULT 'open'
);

CREATE TABLE "additional_drivers" (
  "rental_id" uuid NOT NULL,
  "customer_id" uuid NOT NULL,
  "driver_license_id" uuid NOT NULL,
  PRIMARY KEY ("rental_id", "customer_id")
);

CREATE TABLE "rental_charges" (
  "id" uuid PRIMARY KEY,
  "rental_id" uuid NOT NULL,
  "kind" charge_kind NOT NULL,
  "description" varchar(200) NOT NULL,
  "quantity" numeric(8,2) NOT NULL DEFAULT 1,
  "unit_price_cents" integer NOT NULL,
  "amount_cents" integer NOT NULL,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "payments" (
  "id" uuid PRIMARY KEY,
  "rental_id" uuid,
  "reservation_id" uuid,
  "payment_method_id" uuid NOT NULL,
  "kind" payment_kind NOT NULL,
  "amount_cents" integer NOT NULL,
  "currency" char(3) NOT NULL,
  "processor_reference" varchar(100) NOT NULL,
  "parent_payment_id" uuid,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE INDEX ON "reservations" ("pickup_location_id", "vehicle_class_id", "pickup_at");

CREATE INDEX ON "rentals" ("vehicle_id", "status");

COMMENT ON TABLE "locations" IS 'Branches where cars are picked up and returned.';

COMMENT ON COLUMN "locations"."code" IS 'e.g. an airport code plus a suffix, "DFW01"';

COMMENT ON COLUMN "locations"."timezone" IS 'IANA zone, e.g. America/Chicago. Pickup and return times are shown in it.';

COMMENT ON TABLE "location_hours" IS 'Counter opening hours in the location timezone. Pickups outside them need after-hours approval.';

COMMENT ON COLUMN "location_hours"."day_of_week" IS '1 = Monday ... 7 = Sunday';

COMMENT ON TABLE "staff" IS 'Counter agents, lot staff and managers. Every handover records who did it.';

COMMENT ON TABLE "vehicle_classes" IS 'What customers book. A reservation promises a CLASS; the specific car is chosen at pickup.';

COMMENT ON COLUMN "vehicle_classes"."code" IS 'ACRISS code, e.g. ECMR = economy, 2-4 door, manual, air conditioning';

COMMENT ON COLUMN "vehicle_classes"."name" IS 'e.g. Economy, Midsize SUV';

COMMENT ON TABLE "vehicle_models" IS 'Make and model, grouped into a class. "Toyota Corolla or similar" is this relationship.';

COMMENT ON TABLE "vehicles" IS 'One physical car. status is a cached convenience; the authoritative answer to "is it on rent?" is an open row in rentals.';

COMMENT ON COLUMN "vehicles"."current_location_id" IS 'Null while on rent or in transit';

COMMENT ON TABLE "maintenance_records" IS 'Service, repairs and recalls. An open record keeps the car off the rentable list.';

COMMENT ON TABLE "damage_reports" IS 'Damage found at check-in or inspection. Existing damage is recorded at checkout so a customer is never billed for it.';

COMMENT ON COLUMN "damage_reports"."rental_id" IS 'Null for damage found outside a rental, e.g. in the lot';

COMMENT ON TABLE "damage_photos" IS 'Evidence for a damage report.';

COMMENT ON COLUMN "damage_photos"."storage_key" IS 'Object storage key. Images never live in the database.';

COMMENT ON TABLE "vehicle_transfers" IS 'Fleet rebalancing between locations, and one-way rentals coming home.';

COMMENT ON TABLE "rate_plans" IS 'Price lists by class and pickup location. Reservations snapshot the quote, so editing a plan never changes a confirmed price.';

COMMENT ON COLUMN "rate_plans"."name" IS 'e.g. Standard, Weekend, Prepaid';

COMMENT ON COLUMN "rate_plans"."weekly_rate_cents" IS 'Applied from 5 or more days, when set';

COMMENT ON COLUMN "rate_plans"."included_km_per_day" IS 'Null for unlimited mileage';

COMMENT ON TABLE "extras" IS 'Add-ons sold with a rental.';

COMMENT ON COLUMN "extras"."code" IS 'e.g. GPS, CHILD_SEAT, ADDL_DRIVER, TOLL_PASS';

COMMENT ON TABLE "coverage_products" IS 'Protection products: collision damage waiver, liability, personal accident.';

COMMENT ON COLUMN "coverage_products"."code" IS 'e.g. CDW, SLI, PAI';

COMMENT ON COLUMN "coverage_products"."deductible_cents" IS 'What the customer still owes on a claim; null for none';

COMMENT ON TABLE "customers" IS 'People who book or drive. An additional driver is a customer too.';

COMMENT ON COLUMN "customers"."date_of_birth" IS 'Drives young-driver surcharges and minimum-age rules';

COMMENT ON TABLE "driver_licenses" IS 'Licenses on file. A rental points at the exact license checked at the counter.';

COMMENT ON COLUMN "driver_licenses"."issuing_region" IS 'State or province, where it applies';

COMMENT ON TABLE "payment_methods" IS 'Saved cards, as processor tokens.';

COMMENT ON COLUMN "payment_methods"."processor_token" IS 'Card data stays with the payment processor; only its token is stored';

COMMENT ON TABLE "reservations" IS 'A promise of a vehicle class, at a place and time, for a quoted price. Never a specific car.';

COMMENT ON COLUMN "reservations"."return_location_id" IS 'Different from pickup for a one-way rental';

COMMENT ON COLUMN "reservations"."quoted_total_cents" IS 'Snapshot of the price shown at booking';

COMMENT ON TABLE "reservation_extras" IS 'Add-ons requested with a booking.';

COMMENT ON COLUMN "reservation_extras"."price_cents" IS 'Price at booking';

COMMENT ON TABLE "reservation_coverages" IS 'Protection products chosen at booking. Can still be added or declined at the counter.';

COMMENT ON COLUMN "reservation_coverages"."daily_price_cents" IS 'Price at booking';

COMMENT ON TABLE "rentals" IS 'The rental agreement. Odometer and fuel at both ends are what mileage and refuelling charges are computed from.';

COMMENT ON COLUMN "rentals"."reservation_id" IS 'Null for walk-up rentals';

COMMENT ON COLUMN "rentals"."vehicle_id" IS 'The specific car, assigned at pickup';

COMMENT ON COLUMN "rentals"."fuel_out_eighths" IS 'Fuel level 0-8, as on the checkout sheet';

COMMENT ON TABLE "additional_drivers" IS 'Everyone else authorized to drive on an agreement, with the license that was checked.';

COMMENT ON TABLE "rental_charges" IS 'Every line on the final invoice: time, extras, coverage, mileage, fuel, late return, one-way fee, damage, taxes. The invoice total is their sum.';

COMMENT ON COLUMN "rental_charges"."description" IS 'e.g. "5 days x $42.00", "Refuelling, 3/8 tank"';

COMMENT ON COLUMN "rental_charges"."amount_cents" IS 'Negative for discounts and credits';

COMMENT ON TABLE "payments" IS 'Card activity as separate rows: the deposit authorization at pickup, the capture at return, any release or refund.';

COMMENT ON COLUMN "payments"."reservation_id" IS 'Set for prepaid bookings charged before a rental exists';

COMMENT ON COLUMN "payments"."parent_payment_id" IS 'A capture or release points at its authorization; a refund at its capture';

ALTER TABLE "location_hours" ADD FOREIGN KEY ("location_id") REFERENCES "locations" ("id");

ALTER TABLE "staff" ADD FOREIGN KEY ("location_id") REFERENCES "locations" ("id");

ALTER TABLE "vehicle_models" ADD FOREIGN KEY ("vehicle_class_id") REFERENCES "vehicle_classes" ("id");

ALTER TABLE "vehicles" ADD FOREIGN KEY ("vehicle_model_id") REFERENCES "vehicle_models" ("id");

ALTER TABLE "vehicles" ADD FOREIGN KEY ("home_location_id") REFERENCES "locations" ("id");

ALTER TABLE "vehicles" ADD FOREIGN KEY ("current_location_id") REFERENCES "locations" ("id");

ALTER TABLE "maintenance_records" ADD FOREIGN KEY ("vehicle_id") REFERENCES "vehicles" ("id");

ALTER TABLE "damage_reports" ADD FOREIGN KEY ("vehicle_id") REFERENCES "vehicles" ("id");

ALTER TABLE "damage_reports" ADD FOREIGN KEY ("rental_id") REFERENCES "rentals" ("id");

ALTER TABLE "damage_reports" ADD FOREIGN KEY ("reported_by") REFERENCES "staff" ("id");

ALTER TABLE "damage_photos" ADD FOREIGN KEY ("damage_report_id") REFERENCES "damage_reports" ("id");

ALTER TABLE "vehicle_transfers" ADD FOREIGN KEY ("vehicle_id") REFERENCES "vehicles" ("id");

ALTER TABLE "vehicle_transfers" ADD FOREIGN KEY ("from_location_id") REFERENCES "locations" ("id");

ALTER TABLE "vehicle_transfers" ADD FOREIGN KEY ("to_location_id") REFERENCES "locations" ("id");

ALTER TABLE "vehicle_transfers" ADD FOREIGN KEY ("driver_staff_id") REFERENCES "staff" ("id");

ALTER TABLE "rate_plans" ADD FOREIGN KEY ("vehicle_class_id") REFERENCES "vehicle_classes" ("id");

ALTER TABLE "rate_plans" ADD FOREIGN KEY ("location_id") REFERENCES "locations" ("id");

ALTER TABLE "driver_licenses" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");

ALTER TABLE "driver_licenses" ADD FOREIGN KEY ("verified_by") REFERENCES "staff" ("id");

ALTER TABLE "payment_methods" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");

ALTER TABLE "reservations" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");

ALTER TABLE "reservations" ADD FOREIGN KEY ("vehicle_class_id") REFERENCES "vehicle_classes" ("id");

ALTER TABLE "reservations" ADD FOREIGN KEY ("rate_plan_id") REFERENCES "rate_plans" ("id");

ALTER TABLE "reservations" ADD FOREIGN KEY ("pickup_location_id") REFERENCES "locations" ("id");

ALTER TABLE "reservations" ADD FOREIGN KEY ("return_location_id") REFERENCES "locations" ("id");

ALTER TABLE "reservation_extras" ADD FOREIGN KEY ("reservation_id") REFERENCES "reservations" ("id");

ALTER TABLE "reservation_extras" ADD FOREIGN KEY ("extra_id") REFERENCES "extras" ("id");

ALTER TABLE "reservation_coverages" ADD FOREIGN KEY ("reservation_id") REFERENCES "reservations" ("id");

ALTER TABLE "reservation_coverages" ADD FOREIGN KEY ("coverage_product_id") REFERENCES "coverage_products" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("reservation_id") REFERENCES "reservations" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("driver_license_id") REFERENCES "driver_licenses" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("vehicle_id") REFERENCES "vehicles" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("pickup_location_id") REFERENCES "locations" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("checked_out_by") REFERENCES "staff" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("return_location_id") REFERENCES "locations" ("id");

ALTER TABLE "rentals" ADD FOREIGN KEY ("checked_in_by") REFERENCES "staff" ("id");

ALTER TABLE "additional_drivers" ADD FOREIGN KEY ("rental_id") REFERENCES "rentals" ("id");

ALTER TABLE "additional_drivers" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");

ALTER TABLE "additional_drivers" ADD FOREIGN KEY ("driver_license_id") REFERENCES "driver_licenses" ("id");

ALTER TABLE "rental_charges" ADD FOREIGN KEY ("rental_id") REFERENCES "rentals" ("id");

ALTER TABLE "payments" ADD FOREIGN KEY ("rental_id") REFERENCES "rentals" ("id");

ALTER TABLE "payments" ADD FOREIGN KEY ("reservation_id") REFERENCES "reservations" ("id");

ALTER TABLE "payments" ADD FOREIGN KEY ("payment_method_id") REFERENCES "payment_methods" ("id");

ALTER TABLE "payments" ADD FOREIGN KEY ("parent_payment_id") REFERENCES "payments" ("id");
MySQL
mysql.sql MySQL
CREATE TABLE `locations` (
  `id` uuid PRIMARY KEY,
  `code` varchar(10) UNIQUE NOT NULL COMMENT 'e.g. an airport code plus a suffix, "DFW01"',
  `name` varchar(200) NOT NULL,
  `address_line1` varchar(200) NOT NULL,
  `city` varchar(120) NOT NULL,
  `country` char(2) NOT NULL,
  `timezone` varchar(64) NOT NULL COMMENT 'IANA zone, e.g. America/Chicago. Pickup and return times are shown in it.',
  `is_airport` boolean NOT NULL DEFAULT false
);

CREATE TABLE `location_hours` (
  `location_id` uuid NOT NULL,
  `day_of_week` smallint NOT NULL COMMENT '1 = Monday ... 7 = Sunday',
  `opens_at` time NOT NULL,
  `closes_at` time NOT NULL,
  PRIMARY KEY (`location_id`, `day_of_week`)
);

CREATE TABLE `staff` (
  `id` uuid PRIMARY KEY,
  `location_id` uuid NOT NULL,
  `name` varchar(200) NOT NULL,
  `email` varchar(254) UNIQUE NOT NULL,
  `role` ENUM ('counter_agent', 'lot_attendant', 'mechanic', 'location_manager') NOT NULL
);

CREATE TABLE `vehicle_classes` (
  `id` uuid PRIMARY KEY,
  `code` char(4) UNIQUE NOT NULL COMMENT 'ACRISS code, e.g. ECMR = economy, 2-4 door, manual, air conditioning',
  `name` varchar(100) NOT NULL COMMENT 'e.g. Economy, Midsize SUV',
  `seats` smallint NOT NULL,
  `doors` smallint NOT NULL,
  `transmission` ENUM ('automatic', 'manual') NOT NULL,
  `fuel` ENUM ('petrol', 'diesel', 'hybrid', 'electric') NOT NULL,
  `large_bags` smallint NOT NULL
);

CREATE TABLE `vehicle_models` (
  `id` uuid PRIMARY KEY,
  `vehicle_class_id` uuid NOT NULL,
  `make` varchar(60) NOT NULL,
  `model` varchar(60) NOT NULL,
  `model_year` smallint NOT NULL
);

CREATE TABLE `vehicles` (
  `id` uuid PRIMARY KEY,
  `vin` char(17) UNIQUE NOT NULL,
  `plate` varchar(15) NOT NULL,
  `vehicle_model_id` uuid NOT NULL,
  `color` varchar(30),
  `home_location_id` uuid NOT NULL,
  `current_location_id` uuid COMMENT 'Null while on rent or in transit',
  `status` ENUM ('available', 'reserved', 'on_rent', 'in_transit', 'maintenance', 'out_of_service', 'sold') NOT NULL DEFAULT 'available',
  `odometer_km` integer NOT NULL,
  `in_fleet_on` date NOT NULL,
  `out_of_fleet_on` date
);

CREATE TABLE `maintenance_records` (
  `id` uuid PRIMARY KEY,
  `vehicle_id` uuid NOT NULL,
  `kind` ENUM ('scheduled_service', 'repair', 'tires', 'recall', 'cleaning') NOT NULL,
  `opened_at` timestamptz NOT NULL DEFAULT (now()),
  `closed_at` timestamptz,
  `odometer_km` integer NOT NULL,
  `cost_cents` integer,
  `notes` text
);

CREATE TABLE `damage_reports` (
  `id` uuid PRIMARY KEY,
  `vehicle_id` uuid NOT NULL,
  `rental_id` uuid COMMENT 'Null for damage found outside a rental, e.g. in the lot',
  `reported_by` uuid NOT NULL,
  `reported_at` timestamptz NOT NULL DEFAULT (now()),
  `severity` ENUM ('cosmetic', 'minor', 'major', 'total_loss') NOT NULL,
  `description` text NOT NULL,
  `repaired_at` timestamptz
);

CREATE TABLE `damage_photos` (
  `id` uuid PRIMARY KEY,
  `damage_report_id` uuid NOT NULL,
  `storage_key` varchar(300) NOT NULL COMMENT 'Object storage key. Images never live in the database.',
  `taken_at` timestamptz NOT NULL
);

CREATE TABLE `vehicle_transfers` (
  `id` uuid PRIMARY KEY,
  `vehicle_id` uuid NOT NULL,
  `from_location_id` uuid NOT NULL,
  `to_location_id` uuid NOT NULL,
  `departed_at` timestamptz NOT NULL,
  `arrived_at` timestamptz,
  `driver_staff_id` uuid
);

CREATE TABLE `rate_plans` (
  `id` uuid PRIMARY KEY,
  `vehicle_class_id` uuid NOT NULL,
  `location_id` uuid NOT NULL,
  `name` varchar(100) NOT NULL COMMENT 'e.g. Standard, Weekend, Prepaid',
  `currency` char(3) NOT NULL,
  `daily_rate_cents` integer NOT NULL,
  `weekly_rate_cents` integer COMMENT 'Applied from 5 or more days, when set',
  `included_km_per_day` integer COMMENT 'Null for unlimited mileage',
  `extra_km_cents` integer,
  `valid_from` date NOT NULL,
  `valid_to` date,
  `prepaid` boolean NOT NULL DEFAULT false
);

CREATE TABLE `extras` (
  `id` uuid PRIMARY KEY,
  `code` varchar(20) UNIQUE NOT NULL COMMENT 'e.g. GPS, CHILD_SEAT, ADDL_DRIVER, TOLL_PASS',
  `name` varchar(100) NOT NULL,
  `price_cents` integer NOT NULL,
  `pricing` ENUM ('per_day', 'per_rental') NOT NULL,
  `max_per_rental` smallint NOT NULL DEFAULT 1
);

CREATE TABLE `coverage_products` (
  `id` uuid PRIMARY KEY,
  `code` varchar(20) UNIQUE NOT NULL COMMENT 'e.g. CDW, SLI, PAI',
  `name` varchar(100) NOT NULL,
  `daily_price_cents` integer NOT NULL,
  `deductible_cents` integer COMMENT 'What the customer still owes on a claim; null for none'
);

CREATE TABLE `customers` (
  `id` uuid PRIMARY KEY,
  `email` varchar(254) UNIQUE NOT NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `phone` varchar(30),
  `date_of_birth` date NOT NULL COMMENT 'Drives young-driver surcharges and minimum-age rules',
  `loyalty_number` varchar(20) UNIQUE,
  `do_not_rent` boolean NOT NULL DEFAULT false,
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `driver_licenses` (
  `id` uuid PRIMARY KEY,
  `customer_id` uuid NOT NULL,
  `license_number` varchar(40) NOT NULL,
  `issuing_country` char(2) NOT NULL,
  `issuing_region` varchar(60) COMMENT 'State or province, where it applies',
  `expires_on` date NOT NULL,
  `verified_at` timestamptz,
  `verified_by` uuid
);

CREATE TABLE `payment_methods` (
  `id` uuid PRIMARY KEY,
  `customer_id` uuid NOT NULL,
  `processor_token` varchar(200) NOT NULL COMMENT 'Card data stays with the payment processor; only its token is stored',
  `brand` varchar(20) NOT NULL,
  `last4` char(4) NOT NULL,
  `expires_on` date NOT NULL
);

CREATE TABLE `reservations` (
  `id` uuid PRIMARY KEY,
  `confirmation_code` varchar(12) UNIQUE NOT NULL,
  `customer_id` uuid NOT NULL,
  `vehicle_class_id` uuid NOT NULL,
  `rate_plan_id` uuid NOT NULL,
  `pickup_location_id` uuid NOT NULL,
  `return_location_id` uuid NOT NULL COMMENT 'Different from pickup for a one-way rental',
  `pickup_at` timestamptz NOT NULL,
  `return_at` timestamptz NOT NULL,
  `quoted_total_cents` integer NOT NULL COMMENT 'Snapshot of the price shown at booking',
  `currency` char(3) NOT NULL,
  `status` ENUM ('confirmed', 'cancelled', 'no_show', 'fulfilled') NOT NULL DEFAULT 'confirmed',
  `created_at` timestamptz NOT NULL DEFAULT (now()),
  `cancelled_at` timestamptz
);

CREATE TABLE `reservation_extras` (
  `reservation_id` uuid NOT NULL,
  `extra_id` uuid NOT NULL,
  `quantity` smallint NOT NULL DEFAULT 1,
  `price_cents` integer NOT NULL COMMENT 'Price at booking',
  PRIMARY KEY (`reservation_id`, `extra_id`)
);

CREATE TABLE `reservation_coverages` (
  `reservation_id` uuid NOT NULL,
  `coverage_product_id` uuid NOT NULL,
  `daily_price_cents` integer NOT NULL COMMENT 'Price at booking',
  PRIMARY KEY (`reservation_id`, `coverage_product_id`)
);

CREATE TABLE `rentals` (
  `id` uuid PRIMARY KEY,
  `agreement_number` varchar(20) UNIQUE NOT NULL,
  `reservation_id` uuid UNIQUE COMMENT 'Null for walk-up rentals',
  `customer_id` uuid NOT NULL,
  `driver_license_id` uuid NOT NULL,
  `vehicle_id` uuid NOT NULL COMMENT 'The specific car, assigned at pickup',
  `pickup_location_id` uuid NOT NULL,
  `picked_up_at` timestamptz NOT NULL,
  `odometer_out_km` integer NOT NULL,
  `fuel_out_eighths` smallint NOT NULL COMMENT 'Fuel level 0-8, as on the checkout sheet',
  `checked_out_by` uuid NOT NULL,
  `expected_return_at` timestamptz NOT NULL,
  `return_location_id` uuid,
  `returned_at` timestamptz,
  `odometer_in_km` integer,
  `fuel_in_eighths` smallint,
  `checked_in_by` uuid,
  `status` ENUM ('open', 'closed', 'disputed') NOT NULL DEFAULT 'open'
);

CREATE TABLE `additional_drivers` (
  `rental_id` uuid NOT NULL,
  `customer_id` uuid NOT NULL,
  `driver_license_id` uuid NOT NULL,
  PRIMARY KEY (`rental_id`, `customer_id`)
);

CREATE TABLE `rental_charges` (
  `id` uuid PRIMARY KEY,
  `rental_id` uuid NOT NULL,
  `kind` ENUM ('time', 'extra', 'coverage', 'mileage', 'fuel', 'late_return', 'one_way_fee', 'young_driver', 'damage', 'tax', 'discount') NOT NULL,
  `description` varchar(200) NOT NULL COMMENT 'e.g. "5 days x $42.00", "Refuelling, 3/8 tank"',
  `quantity` numeric(8,2) NOT NULL DEFAULT 1,
  `unit_price_cents` integer NOT NULL,
  `amount_cents` integer NOT NULL COMMENT 'Negative for discounts and credits',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `payments` (
  `id` uuid PRIMARY KEY,
  `rental_id` uuid,
  `reservation_id` uuid COMMENT 'Set for prepaid bookings charged before a rental exists',
  `payment_method_id` uuid NOT NULL,
  `kind` ENUM ('authorization', 'capture', 'release', 'refund') NOT NULL,
  `amount_cents` integer NOT NULL,
  `currency` char(3) NOT NULL,
  `processor_reference` varchar(100) NOT NULL,
  `parent_payment_id` uuid COMMENT 'A capture or release points at its authorization; a refund at its capture',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE INDEX `reservations_index_0` ON `reservations` (`pickup_location_id`, `vehicle_class_id`, `pickup_at`);

CREATE INDEX `rentals_index_1` ON `rentals` (`vehicle_id`, `status`);

ALTER TABLE `locations` COMMENT = 'Branches where cars are picked up and returned.';

ALTER TABLE `location_hours` COMMENT = 'Counter opening hours in the location timezone. Pickups outside them need after-hours approval.';

ALTER TABLE `staff` COMMENT = 'Counter agents, lot staff and managers. Every handover records who did it.';

ALTER TABLE `vehicle_classes` COMMENT = 'What customers book. A reservation promises a CLASS; the specific car is chosen at pickup.';

ALTER TABLE `vehicle_models` COMMENT = 'Make and model, grouped into a class. "Toyota Corolla or similar" is this relationship.';

ALTER TABLE `vehicles` COMMENT = 'One physical car. status is a cached convenience; the authoritative answer to "is it on rent?" is an open row in rentals.';

ALTER TABLE `maintenance_records` COMMENT = 'Service, repairs and recalls. An open record keeps the car off the rentable list.';

ALTER TABLE `damage_reports` COMMENT = 'Damage found at check-in or inspection. Existing damage is recorded at checkout so a customer is never billed for it.';

ALTER TABLE `damage_photos` COMMENT = 'Evidence for a damage report.';

ALTER TABLE `vehicle_transfers` COMMENT = 'Fleet rebalancing between locations, and one-way rentals coming home.';

ALTER TABLE `rate_plans` COMMENT = 'Price lists by class and pickup location. Reservations snapshot the quote, so editing a plan never changes a confirmed price.';

ALTER TABLE `extras` COMMENT = 'Add-ons sold with a rental.';

ALTER TABLE `coverage_products` COMMENT = 'Protection products: collision damage waiver, liability, personal accident.';

ALTER TABLE `customers` COMMENT = 'People who book or drive. An additional driver is a customer too.';

ALTER TABLE `driver_licenses` COMMENT = 'Licenses on file. A rental points at the exact license checked at the counter.';

ALTER TABLE `payment_methods` COMMENT = 'Saved cards, as processor tokens.';

ALTER TABLE `reservations` COMMENT = 'A promise of a vehicle class, at a place and time, for a quoted price. Never a specific car.';

ALTER TABLE `reservation_extras` COMMENT = 'Add-ons requested with a booking.';

ALTER TABLE `reservation_coverages` COMMENT = 'Protection products chosen at booking. Can still be added or declined at the counter.';

ALTER TABLE `rentals` COMMENT = 'The rental agreement. Odometer and fuel at both ends are what mileage and refuelling charges are computed from.';

ALTER TABLE `additional_drivers` COMMENT = 'Everyone else authorized to drive on an agreement, with the license that was checked.';

ALTER TABLE `rental_charges` COMMENT = 'Every line on the final invoice: time, extras, coverage, mileage, fuel, late return, one-way fee, damage, taxes. The invoice total is their sum.';

ALTER TABLE `payments` COMMENT = 'Card activity as separate rows: the deposit authorization at pickup, the capture at return, any release or refund.';

ALTER TABLE `location_hours` ADD FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `staff` ADD FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `vehicle_models` ADD FOREIGN KEY (`vehicle_class_id`) REFERENCES `vehicle_classes` (`id`);

ALTER TABLE `vehicles` ADD FOREIGN KEY (`vehicle_model_id`) REFERENCES `vehicle_models` (`id`);

ALTER TABLE `vehicles` ADD FOREIGN KEY (`home_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `vehicles` ADD FOREIGN KEY (`current_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `maintenance_records` ADD FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`);

ALTER TABLE `damage_reports` ADD FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`);

ALTER TABLE `damage_reports` ADD FOREIGN KEY (`rental_id`) REFERENCES `rentals` (`id`);

ALTER TABLE `damage_reports` ADD FOREIGN KEY (`reported_by`) REFERENCES `staff` (`id`);

ALTER TABLE `damage_photos` ADD FOREIGN KEY (`damage_report_id`) REFERENCES `damage_reports` (`id`);

ALTER TABLE `vehicle_transfers` ADD FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`);

ALTER TABLE `vehicle_transfers` ADD FOREIGN KEY (`from_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `vehicle_transfers` ADD FOREIGN KEY (`to_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `vehicle_transfers` ADD FOREIGN KEY (`driver_staff_id`) REFERENCES `staff` (`id`);

ALTER TABLE `rate_plans` ADD FOREIGN KEY (`vehicle_class_id`) REFERENCES `vehicle_classes` (`id`);

ALTER TABLE `rate_plans` ADD FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `driver_licenses` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);

ALTER TABLE `driver_licenses` ADD FOREIGN KEY (`verified_by`) REFERENCES `staff` (`id`);

ALTER TABLE `payment_methods` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);

ALTER TABLE `reservations` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);

ALTER TABLE `reservations` ADD FOREIGN KEY (`vehicle_class_id`) REFERENCES `vehicle_classes` (`id`);

ALTER TABLE `reservations` ADD FOREIGN KEY (`rate_plan_id`) REFERENCES `rate_plans` (`id`);

ALTER TABLE `reservations` ADD FOREIGN KEY (`pickup_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `reservations` ADD FOREIGN KEY (`return_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `reservation_extras` ADD FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`);

ALTER TABLE `reservation_extras` ADD FOREIGN KEY (`extra_id`) REFERENCES `extras` (`id`);

ALTER TABLE `reservation_coverages` ADD FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`);

ALTER TABLE `reservation_coverages` ADD FOREIGN KEY (`coverage_product_id`) REFERENCES `coverage_products` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`driver_license_id`) REFERENCES `driver_licenses` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`pickup_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`checked_out_by`) REFERENCES `staff` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`return_location_id`) REFERENCES `locations` (`id`);

ALTER TABLE `rentals` ADD FOREIGN KEY (`checked_in_by`) REFERENCES `staff` (`id`);

ALTER TABLE `additional_drivers` ADD FOREIGN KEY (`rental_id`) REFERENCES `rentals` (`id`);

ALTER TABLE `additional_drivers` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`);

ALTER TABLE `additional_drivers` ADD FOREIGN KEY (`driver_license_id`) REFERENCES `driver_licenses` (`id`);

ALTER TABLE `rental_charges` ADD FOREIGN KEY (`rental_id`) REFERENCES `rentals` (`id`);

ALTER TABLE `payments` ADD FOREIGN KEY (`rental_id`) REFERENCES `rentals` (`id`);

ALTER TABLE `payments` ADD FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`);

ALTER TABLE `payments` ADD FOREIGN KEY (`payment_method_id`) REFERENCES `payment_methods` (`id`);

ALTER TABLE `payments` ADD FOREIGN KEY (`parent_payment_id`) REFERENCES `payments` (`id`);
SQL Server
sql-server.sql SQL Server
CREATE TABLE [locations] (
  [id] uuid PRIMARY KEY,
  [code] varchar(10) UNIQUE NOT NULL,
  [name] varchar(200) NOT NULL,
  [address_line1] varchar(200) NOT NULL,
  [city] varchar(120) NOT NULL,
  [country] char(2) NOT NULL,
  [timezone] varchar(64) NOT NULL,
  [is_airport] boolean NOT NULL DEFAULT (false)
)
GO

CREATE TABLE [location_hours] (
  [location_id] uuid NOT NULL,
  [day_of_week] smallint NOT NULL,
  [opens_at] time NOT NULL,
  [closes_at] time NOT NULL,
  PRIMARY KEY ([location_id], [day_of_week])
)
GO

CREATE TABLE [staff] (
  [id] uuid PRIMARY KEY,
  [location_id] uuid NOT NULL,
  [name] varchar(200) NOT NULL,
  [email] varchar(254) UNIQUE NOT NULL,
  [role] nvarchar(255) NOT NULL CHECK ([role] IN ('counter_agent', 'lot_attendant', 'mechanic', 'location_manager'))
)
GO

CREATE TABLE [vehicle_classes] (
  [id] uuid PRIMARY KEY,
  [code] char(4) UNIQUE NOT NULL,
  [name] varchar(100) NOT NULL,
  [seats] smallint NOT NULL,
  [doors] smallint NOT NULL,
  [transmission] nvarchar(255) NOT NULL CHECK ([transmission] IN ('automatic', 'manual')),
  [fuel] nvarchar(255) NOT NULL CHECK ([fuel] IN ('petrol', 'diesel', 'hybrid', 'electric')),
  [large_bags] smallint NOT NULL
)
GO

CREATE TABLE [vehicle_models] (
  [id] uuid PRIMARY KEY,
  [vehicle_class_id] uuid NOT NULL,
  [make] varchar(60) NOT NULL,
  [model] varchar(60) NOT NULL,
  [model_year] smallint NOT NULL
)
GO

CREATE TABLE [vehicles] (
  [id] uuid PRIMARY KEY,
  [vin] char(17) UNIQUE NOT NULL,
  [plate] varchar(15) NOT NULL,
  [vehicle_model_id] uuid NOT NULL,
  [color] varchar(30),
  [home_location_id] uuid NOT NULL,
  [current_location_id] uuid,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('available', 'reserved', 'on_rent', 'in_transit', 'maintenance', 'out_of_service', 'sold')) DEFAULT 'available',
  [odometer_km] integer NOT NULL,
  [in_fleet_on] date NOT NULL,
  [out_of_fleet_on] date
)
GO

CREATE TABLE [maintenance_records] (
  [id] uuid PRIMARY KEY,
  [vehicle_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('scheduled_service', 'repair', 'tires', 'recall', 'cleaning')),
  [opened_at] timestamptz NOT NULL DEFAULT (now()),
  [closed_at] timestamptz,
  [odometer_km] integer NOT NULL,
  [cost_cents] integer,
  [notes] text
)
GO

CREATE TABLE [damage_reports] (
  [id] uuid PRIMARY KEY,
  [vehicle_id] uuid NOT NULL,
  [rental_id] uuid,
  [reported_by] uuid NOT NULL,
  [reported_at] timestamptz NOT NULL DEFAULT (now()),
  [severity] nvarchar(255) NOT NULL CHECK ([severity] IN ('cosmetic', 'minor', 'major', 'total_loss')),
  [description] text NOT NULL,
  [repaired_at] timestamptz
)
GO

CREATE TABLE [damage_photos] (
  [id] uuid PRIMARY KEY,
  [damage_report_id] uuid NOT NULL,
  [storage_key] varchar(300) NOT NULL,
  [taken_at] timestamptz NOT NULL
)
GO

CREATE TABLE [vehicle_transfers] (
  [id] uuid PRIMARY KEY,
  [vehicle_id] uuid NOT NULL,
  [from_location_id] uuid NOT NULL,
  [to_location_id] uuid NOT NULL,
  [departed_at] timestamptz NOT NULL,
  [arrived_at] timestamptz,
  [driver_staff_id] uuid
)
GO

CREATE TABLE [rate_plans] (
  [id] uuid PRIMARY KEY,
  [vehicle_class_id] uuid NOT NULL,
  [location_id] uuid NOT NULL,
  [name] varchar(100) NOT NULL,
  [currency] char(3) NOT NULL,
  [daily_rate_cents] integer NOT NULL,
  [weekly_rate_cents] integer,
  [included_km_per_day] integer,
  [extra_km_cents] integer,
  [valid_from] date NOT NULL,
  [valid_to] date,
  [prepaid] boolean NOT NULL DEFAULT (false)
)
GO

CREATE TABLE [extras] (
  [id] uuid PRIMARY KEY,
  [code] varchar(20) UNIQUE NOT NULL,
  [name] varchar(100) NOT NULL,
  [price_cents] integer NOT NULL,
  [pricing] nvarchar(255) NOT NULL CHECK ([pricing] IN ('per_day', 'per_rental')),
  [max_per_rental] smallint NOT NULL DEFAULT (1)
)
GO

CREATE TABLE [coverage_products] (
  [id] uuid PRIMARY KEY,
  [code] varchar(20) UNIQUE NOT NULL,
  [name] varchar(100) NOT NULL,
  [daily_price_cents] integer NOT NULL,
  [deductible_cents] integer
)
GO

CREATE TABLE [customers] (
  [id] uuid PRIMARY KEY,
  [email] varchar(254) UNIQUE NOT NULL,
  [first_name] varchar(100) NOT NULL,
  [last_name] varchar(100) NOT NULL,
  [phone] varchar(30),
  [date_of_birth] date NOT NULL,
  [loyalty_number] varchar(20) UNIQUE,
  [do_not_rent] boolean NOT NULL DEFAULT (false),
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [driver_licenses] (
  [id] uuid PRIMARY KEY,
  [customer_id] uuid NOT NULL,
  [license_number] varchar(40) NOT NULL,
  [issuing_country] char(2) NOT NULL,
  [issuing_region] varchar(60),
  [expires_on] date NOT NULL,
  [verified_at] timestamptz,
  [verified_by] uuid
)
GO

CREATE TABLE [payment_methods] (
  [id] uuid PRIMARY KEY,
  [customer_id] uuid NOT NULL,
  [processor_token] varchar(200) NOT NULL,
  [brand] varchar(20) NOT NULL,
  [last4] char(4) NOT NULL,
  [expires_on] date NOT NULL
)
GO

CREATE TABLE [reservations] (
  [id] uuid PRIMARY KEY,
  [confirmation_code] varchar(12) UNIQUE NOT NULL,
  [customer_id] uuid NOT NULL,
  [vehicle_class_id] uuid NOT NULL,
  [rate_plan_id] uuid NOT NULL,
  [pickup_location_id] uuid NOT NULL,
  [return_location_id] uuid NOT NULL,
  [pickup_at] timestamptz NOT NULL,
  [return_at] timestamptz NOT NULL,
  [quoted_total_cents] integer NOT NULL,
  [currency] char(3) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('confirmed', 'cancelled', 'no_show', 'fulfilled')) DEFAULT 'confirmed',
  [created_at] timestamptz NOT NULL DEFAULT (now()),
  [cancelled_at] timestamptz
)
GO

CREATE TABLE [reservation_extras] (
  [reservation_id] uuid NOT NULL,
  [extra_id] uuid NOT NULL,
  [quantity] smallint NOT NULL DEFAULT (1),
  [price_cents] integer NOT NULL,
  PRIMARY KEY ([reservation_id], [extra_id])
)
GO

CREATE TABLE [reservation_coverages] (
  [reservation_id] uuid NOT NULL,
  [coverage_product_id] uuid NOT NULL,
  [daily_price_cents] integer NOT NULL,
  PRIMARY KEY ([reservation_id], [coverage_product_id])
)
GO

CREATE TABLE [rentals] (
  [id] uuid PRIMARY KEY,
  [agreement_number] varchar(20) UNIQUE NOT NULL,
  [reservation_id] uuid UNIQUE,
  [customer_id] uuid NOT NULL,
  [driver_license_id] uuid NOT NULL,
  [vehicle_id] uuid NOT NULL,
  [pickup_location_id] uuid NOT NULL,
  [picked_up_at] timestamptz NOT NULL,
  [odometer_out_km] integer NOT NULL,
  [fuel_out_eighths] smallint NOT NULL,
  [checked_out_by] uuid NOT NULL,
  [expected_return_at] timestamptz NOT NULL,
  [return_location_id] uuid,
  [returned_at] timestamptz,
  [odometer_in_km] integer,
  [fuel_in_eighths] smallint,
  [checked_in_by] uuid,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('open', 'closed', 'disputed')) DEFAULT 'open'
)
GO

CREATE TABLE [additional_drivers] (
  [rental_id] uuid NOT NULL,
  [customer_id] uuid NOT NULL,
  [driver_license_id] uuid NOT NULL,
  PRIMARY KEY ([rental_id], [customer_id])
)
GO

CREATE TABLE [rental_charges] (
  [id] uuid PRIMARY KEY,
  [rental_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('time', 'extra', 'coverage', 'mileage', 'fuel', 'late_return', 'one_way_fee', 'young_driver', 'damage', 'tax', 'discount')),
  [description] varchar(200) NOT NULL,
  [quantity] numeric(8,2) NOT NULL DEFAULT (1),
  [unit_price_cents] integer NOT NULL,
  [amount_cents] integer NOT NULL,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [payments] (
  [id] uuid PRIMARY KEY,
  [rental_id] uuid,
  [reservation_id] uuid,
  [payment_method_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('authorization', 'capture', 'release', 'refund')),
  [amount_cents] integer NOT NULL,
  [currency] char(3) NOT NULL,
  [processor_reference] varchar(100) NOT NULL,
  [parent_payment_id] uuid,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE INDEX [reservations_index_0] ON [reservations] ("pickup_location_id", "vehicle_class_id", "pickup_at")
GO

CREATE INDEX [rentals_index_1] ON [rentals] ("vehicle_id", "status")
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Branches where cars are picked up and returned.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'locations';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. an airport code plus a suffix, "DFW01"',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'locations',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'IANA zone, e.g. America/Chicago. Pickup and return times are shown in it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'locations',
@level2type = N'Column', @level2name = 'timezone';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Counter opening hours in the location timezone. Pickups outside them need after-hours approval.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'location_hours';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '1 = Monday ... 7 = Sunday',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'location_hours',
@level2type = N'Column', @level2name = 'day_of_week';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Counter agents, lot staff and managers. Every handover records who did it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'staff';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'What customers book. A reservation promises a CLASS; the specific car is chosen at pickup.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicle_classes';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'ACRISS code, e.g. ECMR = economy, 2-4 door, manual, air conditioning',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicle_classes',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Economy, Midsize SUV',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicle_classes',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Make and model, grouped into a class. "Toyota Corolla or similar" is this relationship.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicle_models';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One physical car. status is a cached convenience; the authoritative answer to "is it on rent?" is an open row in rentals.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicles';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null while on rent or in transit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicles',
@level2type = N'Column', @level2name = 'current_location_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Service, repairs and recalls. An open record keeps the car off the rentable list.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'maintenance_records';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Damage found at check-in or inspection. Existing damage is recorded at checkout so a customer is never billed for it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'damage_reports';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for damage found outside a rental, e.g. in the lot',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'damage_reports',
@level2type = N'Column', @level2name = 'rental_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Evidence for a damage report.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'damage_photos';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Object storage key. Images never live in the database.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'damage_photos',
@level2type = N'Column', @level2name = 'storage_key';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Fleet rebalancing between locations, and one-way rentals coming home.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'vehicle_transfers';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Price lists by class and pickup location. Reservations snapshot the quote, so editing a plan never changes a confirmed price.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rate_plans';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Standard, Weekend, Prepaid',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rate_plans',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Applied from 5 or more days, when set',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rate_plans',
@level2type = N'Column', @level2name = 'weekly_rate_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for unlimited mileage',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rate_plans',
@level2type = N'Column', @level2name = 'included_km_per_day';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Add-ons sold with a rental.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'extras';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. GPS, CHILD_SEAT, ADDL_DRIVER, TOLL_PASS',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'extras',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Protection products: collision damage waiver, liability, personal accident.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'coverage_products';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. CDW, SLI, PAI',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'coverage_products',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'What the customer still owes on a claim; null for none',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'coverage_products',
@level2type = N'Column', @level2name = 'deductible_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'People who book or drive. An additional driver is a customer too.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'customers';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Drives young-driver surcharges and minimum-age rules',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'customers',
@level2type = N'Column', @level2name = 'date_of_birth';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Licenses on file. A rental points at the exact license checked at the counter.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'driver_licenses';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'State or province, where it applies',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'driver_licenses',
@level2type = N'Column', @level2name = 'issuing_region';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Saved cards, as processor tokens.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payment_methods';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Card data stays with the payment processor; only its token is stored',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payment_methods',
@level2type = N'Column', @level2name = 'processor_token';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A promise of a vehicle class, at a place and time, for a quoted price. Never a specific car.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservations';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Different from pickup for a one-way rental',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservations',
@level2type = N'Column', @level2name = 'return_location_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Snapshot of the price shown at booking',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservations',
@level2type = N'Column', @level2name = 'quoted_total_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Add-ons requested with a booking.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservation_extras';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Price at booking',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservation_extras',
@level2type = N'Column', @level2name = 'price_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Protection products chosen at booking. Can still be added or declined at the counter.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservation_coverages';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Price at booking',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'reservation_coverages',
@level2type = N'Column', @level2name = 'daily_price_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The rental agreement. Odometer and fuel at both ends are what mileage and refuelling charges are computed from.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rentals';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for walk-up rentals',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rentals',
@level2type = N'Column', @level2name = 'reservation_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The specific car, assigned at pickup',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rentals',
@level2type = N'Column', @level2name = 'vehicle_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Fuel level 0-8, as on the checkout sheet',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rentals',
@level2type = N'Column', @level2name = 'fuel_out_eighths';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Everyone else authorized to drive on an agreement, with the license that was checked.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'additional_drivers';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Every line on the final invoice: time, extras, coverage, mileage, fuel, late return, one-way fee, damage, taxes. The invoice total is their sum.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rental_charges';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. "5 days x $42.00", "Refuelling, 3/8 tank"',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rental_charges',
@level2type = N'Column', @level2name = 'description';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Negative for discounts and credits',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'rental_charges',
@level2type = N'Column', @level2name = 'amount_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Card activity as separate rows: the deposit authorization at pickup, the capture at return, any release or refund.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payments';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set for prepaid bookings charged before a rental exists',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payments',
@level2type = N'Column', @level2name = 'reservation_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'A capture or release points at its authorization; a refund at its capture',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payments',
@level2type = N'Column', @level2name = 'parent_payment_id';
GO

ALTER TABLE [location_hours] ADD FOREIGN KEY ([location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [staff] ADD FOREIGN KEY ([location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [vehicle_models] ADD FOREIGN KEY ([vehicle_class_id]) REFERENCES [vehicle_classes] ([id])
GO

ALTER TABLE [vehicles] ADD FOREIGN KEY ([vehicle_model_id]) REFERENCES [vehicle_models] ([id])
GO

ALTER TABLE [vehicles] ADD FOREIGN KEY ([home_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [vehicles] ADD FOREIGN KEY ([current_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [maintenance_records] ADD FOREIGN KEY ([vehicle_id]) REFERENCES [vehicles] ([id])
GO

ALTER TABLE [damage_reports] ADD FOREIGN KEY ([vehicle_id]) REFERENCES [vehicles] ([id])
GO

ALTER TABLE [damage_reports] ADD FOREIGN KEY ([rental_id]) REFERENCES [rentals] ([id])
GO

ALTER TABLE [damage_reports] ADD FOREIGN KEY ([reported_by]) REFERENCES [staff] ([id])
GO

ALTER TABLE [damage_photos] ADD FOREIGN KEY ([damage_report_id]) REFERENCES [damage_reports] ([id])
GO

ALTER TABLE [vehicle_transfers] ADD FOREIGN KEY ([vehicle_id]) REFERENCES [vehicles] ([id])
GO

ALTER TABLE [vehicle_transfers] ADD FOREIGN KEY ([from_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [vehicle_transfers] ADD FOREIGN KEY ([to_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [vehicle_transfers] ADD FOREIGN KEY ([driver_staff_id]) REFERENCES [staff] ([id])
GO

ALTER TABLE [rate_plans] ADD FOREIGN KEY ([vehicle_class_id]) REFERENCES [vehicle_classes] ([id])
GO

ALTER TABLE [rate_plans] ADD FOREIGN KEY ([location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [driver_licenses] ADD FOREIGN KEY ([customer_id]) REFERENCES [customers] ([id])
GO

ALTER TABLE [driver_licenses] ADD FOREIGN KEY ([verified_by]) REFERENCES [staff] ([id])
GO

ALTER TABLE [payment_methods] ADD FOREIGN KEY ([customer_id]) REFERENCES [customers] ([id])
GO

ALTER TABLE [reservations] ADD FOREIGN KEY ([customer_id]) REFERENCES [customers] ([id])
GO

ALTER TABLE [reservations] ADD FOREIGN KEY ([vehicle_class_id]) REFERENCES [vehicle_classes] ([id])
GO

ALTER TABLE [reservations] ADD FOREIGN KEY ([rate_plan_id]) REFERENCES [rate_plans] ([id])
GO

ALTER TABLE [reservations] ADD FOREIGN KEY ([pickup_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [reservations] ADD FOREIGN KEY ([return_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [reservation_extras] ADD FOREIGN KEY ([reservation_id]) REFERENCES [reservations] ([id])
GO

ALTER TABLE [reservation_extras] ADD FOREIGN KEY ([extra_id]) REFERENCES [extras] ([id])
GO

ALTER TABLE [reservation_coverages] ADD FOREIGN KEY ([reservation_id]) REFERENCES [reservations] ([id])
GO

ALTER TABLE [reservation_coverages] ADD FOREIGN KEY ([coverage_product_id]) REFERENCES [coverage_products] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([reservation_id]) REFERENCES [reservations] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([customer_id]) REFERENCES [customers] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([driver_license_id]) REFERENCES [driver_licenses] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([vehicle_id]) REFERENCES [vehicles] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([pickup_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([checked_out_by]) REFERENCES [staff] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([return_location_id]) REFERENCES [locations] ([id])
GO

ALTER TABLE [rentals] ADD FOREIGN KEY ([checked_in_by]) REFERENCES [staff] ([id])
GO

ALTER TABLE [additional_drivers] ADD FOREIGN KEY ([rental_id]) REFERENCES [rentals] ([id])
GO

ALTER TABLE [additional_drivers] ADD FOREIGN KEY ([customer_id]) REFERENCES [customers] ([id])
GO

ALTER TABLE [additional_drivers] ADD FOREIGN KEY ([driver_license_id]) REFERENCES [driver_licenses] ([id])
GO

ALTER TABLE [rental_charges] ADD FOREIGN KEY ([rental_id]) REFERENCES [rentals] ([id])
GO

ALTER TABLE [payments] ADD FOREIGN KEY ([rental_id]) REFERENCES [rentals] ([id])
GO

ALTER TABLE [payments] ADD FOREIGN KEY ([reservation_id]) REFERENCES [reservations] ([id])
GO

ALTER TABLE [payments] ADD FOREIGN KEY ([payment_method_id]) REFERENCES [payment_methods] ([id])
GO

ALTER TABLE [payments] ADD FOREIGN KEY ([parent_payment_id]) REFERENCES [payments] ([id])
GO

How to use this template

  1. Open the ER diagram to see the tables and how they relate.
  2. Copy it into your workspace. You get your own editable copy, in DBML.
  3. Export it as PostgreSQL, MySQL, or SQL Server DDL, or as Prisma, Drizzle, SQLAlchemy, TypeScript, Zod or C# models.

The copy opens in DB Planner’s ER diagram tool, with the schema as text in the DBML editor beside it. Have a database already? Turn its SQL into an ER diagram instead.

Frequently asked questions

Which table is at the centre of the Car Rental database schema?
locations. It is referenced by 7 other tables: location_hours, rate_plans, rentals, reservations, staff and 2 more.
How is location_hours related to locations?
location_hours.location_id references locations.id, a many to one relationship. All 45 relationships in the schema are listed on this page.
Which table in the Car Rental database schema has the most columns?
rentals, with 18 columns, followed by reservations (14) and rate_plans (12). The schema has 174 columns across 23 tables.
Can I export it to PostgreSQL or MySQL?
Yes. Every DB Planner template exports as DBML, as PostgreSQL, MySQL, or SQL Server DDL, and as C# models.
Is this template free?
Yes. You can open it in the browser without an account, and copy it into your own workspace with a free DB Planner account.
Can I change it after copying it?
Yes. A copy is yours: you can rename tables, add columns, and export it. Changes the author makes later do not affect your copy.