0% completed
In this case study, we'll explore the database modeling of a Hotel Management System using Entity-Relationship (ER) design and then map it to a relational database schema. By following this guide, you'll gain a comprehensive understanding of how to model databases for complex systems like hotels.
Understanding the system's requirements is the first and most crucial step. It ensures that the database will support all necessary functionalities.
For our case study, let's outline the key functionalities that the HMS should support:
We will build the ER diagram for the Hospital Management System through the following four steps.
Entities represent objects or concepts in the system that have data stored about them.
We'll define attributes for each entity, including primary keys (PK) and foreign keys (FK).
Here is the final ER diagram.
When converting the ER diagram into a relational schema for our Hotel Management System, we need to carefully handle various components to ensure data integrity and optimal performance. Here are the key considerations:
Entity Tables and Attributes:
Phone_Number
.Foreign Keys and Relationships:
Handling Many-to-Many Relationships:
Here is the final relational schema diagram.
Now, let's translate the ER model into SQL tables.
CREATE TABLE Guest ( Guest_ID INT PRIMARY KEY, First_Name VARCHAR(50), Last_Name VARCHAR(50), Date_of_Birth DATE, Gender VARCHAR(10), Address VARCHAR(100), Email VARCHAR(50), Identification_Type VARCHAR(20), Identification_Number VARCHAR(50) );
CREATE TABLE Guest_Phone_Number ( Guest_ID INT, Phone_Number VARCHAR(15), PRIMARY KEY (Guest_ID, Phone_Number), FOREIGN KEY (Guest_ID) REFERENCES Guest(Guest_ID) );
CREATE TABLE Room_Type ( Room_Type_ID INT PRIMARY KEY, Type_Name VARCHAR(20), Description TEXT, Price_Per_Night DECIMAL(10, 2), Max_Occupancy INT );
CREATE TABLE Room ( Room_ID INT PRIMARY KEY, Room_Number VARCHAR(10), Floor INT, Status VARCHAR(20), -- e.g., Available, Occupied, Maintenance Room_Type_ID INT, Department_ID INT, FOREIGN KEY (Room_Type_ID) REFERENCES Room_Type(Room_Type_ID), FOREIGN KEY (Department_ID) REFERENCES Department(Department_ID) );
CREATE TABLE Reservation ( Reservation_ID INT PRIMARY KEY, Check_In_Date DATE, Check_Out_Date DATE, Number_of_Guests INT, Reservation_Status VARCHAR(20), -- e.g., Booked, Checked-In, Checked-Out, Cancelled Guest_ID INT, FOREIGN KEY (Guest_ID) REFERENCES Guest(Guest_ID) );
CREATE TABLE Reservation_Room ( Reservation_ID INT, Room_ID INT, Rate_Applied DECIMAL(10, 2), Notes TEXT, PRIMARY KEY (Reservation_ID, Room_ID), FOREIGN KEY (Reservation_ID) REFERENCES Reservation(Reservation_ID), FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID) );
CREATE TABLE Payment ( Payment_ID INT PRIMARY KEY, Payment_Date DATE, Amount DECIMAL(10, 2), Payment_Method VARCHAR(20), -- e.g., Credit Card, Cash Reservation_ID INT, FOREIGN KEY (Reservation_ID) REFERENCES Reservation(Reservation_ID) );
CREATE TABLE Staff ( Staff_ID INT PRIMARY KEY, First_Name VARCHAR(50), Last_Name VARCHAR(50), Role VARCHAR(50), Email VARCHAR(50), Department_ID INT, FOREIGN KEY (Department_ID) REFERENCES Department(Department_ID) );
CREATE TABLE Staff_Phone_Number ( Staff_ID INT, Phone_Number VARCHAR(15), PRIMARY KEY (Staff_ID, Phone_Number), FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_ID) );
CREATE TABLE Department ( Department_ID INT PRIMARY KEY, Department_Name VARCHAR(50), Description TEXT );
CREATE TABLE Service ( Service_ID INT PRIMARY KEY, Service_Name VARCHAR(50), Description TEXT, Price DECIMAL(10, 2) );
CREATE TABLE Guest_Service ( Guest_ID INT, Service_ID INT, Reservation_ID INT, Quantity INT, Total_Cost DECIMAL(10, 2), PRIMARY KEY (Guest_ID, Service_ID, Reservation_ID), FOREIGN KEY (Guest_ID) REFERENCES Guest(Guest_ID), FOREIGN KEY (Service_ID) REFERENCES Service(Service_ID), FOREIGN KEY (Reservation_ID) REFERENCES Reservation(Reservation_ID) );
.....
.....
.....