我目前正试图解决一个问题,但我陷入了困境。下面是给出的。我正在尝试使用完整性增强创建预订表,但在约束条件下,同一个房间不能再预订。
酒店(hotelNo,hotelName,city)房间(roomNo,hotelNo,类型,价格)预订(hotelNo,guestNo,dateFrom,dateTo,roomNo)客人(guestNo,guestName,guestAddress)酒店包含酒店详情和hotelNo是首要的关键。房间包含每个酒店的房间详细信息,(roomNo,hoteINo)构成主键。预订包含预订的详细信息,(hoteINo,guestNo,dateFrom)构成主键。访客包含访客详细信息,guestNo是主键..
这是我走了多远:
CREATE TABLE Booking(
hotelNo HotelNumbers NOT NULL,
guestNo GuestNumbers NOT NULL,
dateFrom BookingDate NOT NULL,
dateTo BookingDate NULL,
roomNo RoomNumber NOT NULL,
PRIMARY KEY (hotelNo, guestNo, dateFrom),
FOREIGN KEY (hotelNo) REFERENCES Hotel
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (guestNo) REFERENCES Guest
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (hotelNo, roomNo) REFERENCES Room
ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT RoomBooked
CHECK (NOT EXISTS ( SELECT *
FROM Booking b
WHERE b.dateTo > Booking.dateFrom AND
b.dateFrom < Booking.dateTo AND
b.roomNo = Booking.roomNo AND
b.hotelNo = Booking.hotelNo)),
CONSTRAINT GuestBooked
CHECK (NOT EXISTS ( ...............)
答案 0 :(得分:0)
我认为,您需要从4个字段预订pk:hotelNo,guestNo,dateFrom,roomNo;你必须像这样修改CONSTRAINT RoomBooked:
CREATE TABLE Booking(
hotelNo HotelNumbers NOT NULL,
guestNo GuestNumbers NOT NULL,
dateFrom BookingDate NOT NULL,
dateTo BookingDate NULL,
roomNo RoomNumber NOT NULL,
PRIMARY KEY (hotelNo, guestNo, dateFrom, roomNo),
FOREIGN KEY (hotelNo) REFERENCES Hotel
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (guestNo) REFERENCES Guest
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (hotelNo, roomNo) REFERENCES Room
ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT RoomBooked
CHECK (NOT EXISTS (
SELECT 1
FROM Booking b
WHERE (
isnull(b.dateTo, b.dateFrom)
between Booking.dateFrom and isnull(Booking.dateTo, Booking.dateFrom)
or b.dateFrom
between Booking.dateFrom and isnull(Booking.dateTo, Booking.dateFrom))
AND b.roomNo = Booking.roomNo
AND b.hotelNo = Booking.hotelNo)))
我认为,一位客人可以预订多个房间,因此您无法为GuestBooked设置CONSTRAINT