How can exclusion constraints be implemented in MySQL to prevent overlapping reservations in a restaurant booking system?
To prevent overlapping reservations in a restaurant booking system, exclusion constraints can be implemented in MySQL by creating a unique index on the reservation table that includes both the start and end times of the reservation. This will ensure that no two reservations can have overlapping time periods.
// Create a unique index on the reservation table to prevent overlapping reservations
$sql = "ALTER TABLE reservations ADD CONSTRAINT unique_reservation_time UNIQUE (table_id, start_time, end_time)";
$result = mysqli_query($conn, $sql);
if($result) {
echo "Exclusion constraint successfully implemented to prevent overlapping reservations.";
} else {
echo "Error implementing exclusion constraint.";
}
Keywords
Related Questions
- What are the best practices for resizing images stored as BLOBs in a MySQL database using PHP functions like getimagesize and imagerezise?
- What are best practices for efficiently managing and displaying multiple rating boxes on a website using PHP?
- What are common compatibility issues with PHP scripts in different versions of Internet Explorer?