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.";
}