嗨,我想创建一个数据库,其中包含有关酒店的数据。问题是与订单表和房间表的关系。我想查看在给定时间可用的房间。我知道“订单”表上的关系有问题。我可以添加主键作为组合键,同时通过指向表室的组合外键将它们连接吗?
当我尝试运行此代码时:
SELECT * FROM room LEFT JOIN order ON (room.hotelname=order.hotelname) AND (room.roomnr=order.roomnr)
结果是重复表。感谢您的关注。我期待着您的回复!
CREATE TABLE hotel(
hotelname varchar(50),
location varchar(120),
PRIMARY KEY (hotelname)
);
CREATE TABLE roomtype(
roomtype varchar(50),
PRIMARY KEY (roomtype)
);
CREATE TABLE hotelroomtype(
hotellname varchar(50),
roomtype varchar(50),
NumberOfRoom int,
PRIMARY KEY (hotelname, roomtype),
CONSTRAINT FK_hotelname FOREIGN KEY (hotelname) REFERENCES hotel(hotelname),
CONSTRAINT FK_roomtype FOREIGN KEY (roomtype) REFERENCES roomtype(roomtype)
);
CREATE TABLE room(
hotelname varchar(50),
roomtype varchar (50),
romnr int,
PRIMARY KEY (hotelname, romnr),
CONSTRAINT FK_hotelnameroomtype FOREIGN KEY (hotelname,roomtype) REFERENCES hotelroomtype(hotelname,roomtype)
);
CREATE TABLE user(
username varchar(50),
password varchar (50),
userrole varchar(50),
PRIMARY KEY (username)
);
CREATE TABLE order(
hotelname VARCHAR (50),
roomtype varchar (50),
roomnr int,
username varchar(50),
dateArrival date,
dateDeparture date,
PRIMARY KEY (hotelname, roomnr);
add CONSTRAINT FOREIGN KEY (hotelname,roomnr) REFERENCES room(hotelname ,roomnr);
add CONSTRAINT FOREIGN KEY (roomtype) REFERENCES roomtype(roomtype);
add CONSTRAINT FOREIGN KEY (username) REFERENCES user(username);
);
INSERT INTO hotell values("grand hotel oslo", "oslo"),("hotel klubben tønsberg", "Tønsberg"),("radisson blu gardermoen", "ullensarker");
INSERT INTO romtype VALUES ("enkeltrom"),("dobbeltrom"),("familierom"),("suite");
INSERT INTO hotellromtype VALUES ("grand hotel oslo", "enkeltrom", 50), ("grand hotel oslo", "dobbeltrom", 200), ("grand hotel oslo", "suite", 10), ("hotel klubben tønsberg", "enkeltrom", 10), ("hotel klubben tønsberg", "dobbeltrom", 150), ("hotel klubben tønsberg", "familierom",50);
INSERT INTO rom VALUES ("grand hotel oslo", "enkeltrom", 101),("grand hotel oslo", "enkeltrom", 102),("grand hotel oslo", "enkeltrom", 103),("grand hotel oslo", "dobbeltrom", 201),("grand hotel oslo", "dobbeltrom", 202),("grand hotel oslo", "dobbeltrom", 203);
INSERT INTO bruker values ("a", "a","admin" );
INSERT INTO bestilling values ('1','grand hotel oslo','enkeltrom' ,102,'a', '03-08-2019', '08-09-2019');