I am trying to get started with Web Development by trying to develop a Hotel Management System.
I am using MySQL 5.5.44 (Raspberry Pi Debian) and PHP PDOs for the Communication.
The Foreign Keys and Primary Keys are confusing the living hell out of me.
This is my DB Design (Graph created via DbVisualizer)
The Problems are:
I have an Option in the Members Area of the Website where a User can Cancel a Reservation. To do so I want to copy the cancelled Booking (Row in the Bookings Table) into the Cancellation Table and that works BUT after doing that i want to delete the cancelled Booking from Bookings Table. Now this is where my Dilemma starts. I cannot delete a Parent Row from the Bookings Table as the Booking ID in the Cancellation Table AND the Payments Table are connected via References (FK and PK).
I need the BookingID to be the PK as it is the only unique value in the entire System. A Customer might have a unique ID but he can have multiple Bookings and a PK must be unique in the Table. Multiple Bookings per Customer allow the same CustomerID to be present multiple times in the Table. But a BookingID is always unique, even if it is the same Customer. That is why i need to have the Booking ID.
How do i get this to work?
How can I keep BookingID as FK and PK in the Payments and Cancellation Tables but still get rid of the row in the Bookings Table?
Do i have to redesign the DB-Schema? If Yes, how would you do it?
Here are the DESCRIBE
s of the Tables:
mysql> describe Bookings;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| BookingID | int(11) | NO | PRI | 0 | |
| Arrival | datetime | YES | | NULL | |
| Checkout | datetime | YES | | NULL | |
| RoomNumber | int(11) | YES | MUL | NULL | |
| CustomerID | int(11) | YES | MUL | NULL | |
| Breakfast | int(11) | YES | | NULL | |
| Nights | int(11) | YES | | NULL | |
| Comment | varchar(400) | YES | | NULL | |
| BookType | varchar(50) | YES | MUL | NULL | |
| BookTime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)
mysql> describe Cancellations;
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| BookingID | int(11) | NO | PRI | 0 | |
| Arrival | datetime | YES | | NULL | |
| Checkout | datetime | YES | | NULL | |
| RoomNumber | int(11) | YES | MUL | NULL | |
| CustomerID | int(11) | YES | MUL | NULL | |
| Breakfast | int(11) | YES | | NULL | |
| Nights | int(11) | YES | | NULL | |
| BookTime | datetime | YES | | NULL | |
| CancelTime | datetime | YES | | NULL | |
+------------+----------+------+-----+---------+-------+
9 rows in set (0.00 sec)
mysql> describe Payments;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| BookingID | int(11) | NO | PRI | 0 | |
| CustomerID | int(11) | YES | MUL | NULL | |
| Amount | decimal(10,0) | YES | | NULL | |
| Paid | varchar(10) | YES | | NULL | |
| PayTime | datetime | YES | | NULL | |
| Invoice | varchar(50) | YES | | NULL | |
| Cancelled | varchar(10) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
I hope you guys can help a Beginner with his ambitious Project.
Thank You.
I would add a datetime cancelled field and include it in the booking table. I'm sure you will want to keep track of when it was cancelled anyway. Then having Boolean for it also would just be redundant. It's really just a preference though, some people think the Boolean values makes the SQL easier to read, but you would have to add constraints like having a cancelled reservation without a time stamp.
Drop the cancellations
table completely, then add one of the following to the bookings
table:
cancelled_at
timestamp nullable
SELECT * FROM bookings WHERE cancelled_at IS NULL
is_cancelled
boolean default false
SELECT * FROM bookings WHERE is_cancelled = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.