Berikut adalah contoh yang telah disesuaikan dengan DDL yang kamu berikan:
id
SERIAL PRIMARY KEYname
VARCHAR(100) NOT NULLemail
VARCHAR(100) UNIQUE NOT NULLpassword
VARCHAR(60) NOT NULLphone
VARCHAR(14) UNIQUE NOT NULLrole
ENUM("customer", "hotel_owner") NOT NULL
id
SERIAL PRIMARY KEYname
VARCHAR(100) NOT NULLdescription
TEXTaddress
VARCHAR(255) NOT NULLcity
VARCHAR(100) NOT NULLzipcode
VARCHAR(20) NOT NULLcountry
VARCHAR(100) NOT NULLphone
VARCHAR(14) UNIQUE NOT NULLemail
VARCHAR(100) UNIQUE NOT NULLstar
INT DEFAULT 1
id
SERIAL PRIMARY KEYhotel_id
INT REFERENCES hotels(id) ON DELETE CASCADEname
VARCHAR(100) NOT NULLdescription
TEXTprice
DECIMAL(10, 2) CHECK(price >= 0) NOT NULLroom_size
DECIMAL(4, 1) NOT NULLguest
INT CHECK(guest >= 0) NOT NULL
room_type_id
INT PRIMARY KEY REFERENCES room_types(id) ON DELETE CASCADEdouble_bed
INT DEFAULT 0single_bed
INT DEFAULT 0king_bed
INT DEFAULT 0
room_type_id
INT PRIMARY KEY REFERENCES room_types(id) ON DELETE CASCADEhas_shower
BOOLEAN DEFAULT FALSEhas_refrigerator
BOOLEAN DEFAULT FALSEseating_area
BOOLEAN DEFAULT FALSEair_conditioning
BOOLEAN DEFAULT FALSEhas_breakfast
BOOLEAN DEFAULT FALSEhas_wifi
BOOLEAN DEFAULT FALSEsmoking_allowed
BOOLEAN DEFAULT FALSE
id
SERIAL PRIMARY KEYroom_type_id
INT REFERENCES room_types(id) ON DELETE CASCADEroom_number
INT NOT NULLstatus
ENUM("available", "booked", "maintenance") DEFAULT "available"
id
SERIAL PRIMARY KEYuser_id
INT REFERENCES users(id) ON DELETE CASCADEhotel_id
INT REFERENCES hotels(id) ON DELETE CASCADEroom_id
INT REFERENCES rooms(id) ON DELETE CASCADEcheck_in_date
TIMESTAMP NOT NULLcheck_out_date
TIMESTAMP NOT NULLtotal_price
DECIMAL(10, 2) NOT NULLstatus
ENUM("booked", "cancelled", "completed") DEFAULT "booked"
booking_id
INT PRIMARY KEY REFERENCES bookings(id) ON DELETE CASCADExendit_invoice_id
TEXT NOT NULLamount
DECIMAL(10, 2) NOT NULLstatus
ENUM("PENDING", "PAID", "EXPIRED") DEFAULT "PENDING"
invoice_id
INT PRIMARY KEY REFERENCES invoices(booking_id) ON DELETE CASCADEpayment_method
TEXT NOT NULLpaid_amount
DECIMAL(10, 2) NOT NULLpaid_at
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
user_id
INT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADEbalance
DECIMAL(10, 2) DEFAULT 0
- Type: One to Many
- Description: One user can make many bookings, but each booking is associated with only one user.
- Type: One to Many
- Description: One hotel can have many room types, but each room type is associated with only one hotel.
- Type: One to Many
- Description: One room type can be associated with many rooms, but each room is associated with only one room type.
- Type: One to One
- Description: Each room type can have only one set of bed types, and each set of bed types is associated with only one room type.
- Type: One to One
- Description: Each room type can have only one set of facilities, and each set of facilities is associated with only one room type.
- Type: One to One
- Description: Each booking has only one invoice, and each invoice is associated with only one booking.
- Type: One to Many
- Description: One invoice can have many payments, but each payment is associated with only one invoice.
- Type: One to One
- Description: One user has only one balance, and each balance is associated with only one user.
- The
email
in theusers
entity must be unique. - All attributes must be non-null (
NOT NULL
). - Foreign keys (FK) in related entities such as
hotel_id
,room_type_id
,room_id
,user_id
,booking_id
, andinvoice_id
must reference valid primary keys.
- The system allows hotel owners to define different types of rooms with specific bed configurations and facilities.
- Customers can book rooms, and their bookings generate invoices that can be paid through various methods.
- The system also allows tracking of room availability based on bookings and other statuses like maintenance.
- Each column must contain atomic values, and each entity must have a primary key.
- This is fulfilled since all attributes are atomic, and each entity has a primary key.
- The entity must be in 1NF, and all non-prime attributes must fully depend on the primary key.
- All entities are in 1NF.
- All non-prime attributes in each entity fully depend on the primary key.
- The entity must be in 2NF, and there must be no transitive dependency of non-prime attributes on the primary key.
- All entities are in 2NF.
- There are no transitive dependencies in these entities, as all non-prime attributes directly depend on the primary key.