您可以創建結合RoomID和EquipmentType像這樣的表RoomEquipment:
create table RoomEquipment (
RoomID int,
EquipmentType nvarchar(1),
primary key (roomid, equipmenttype),
constraint fk_roomequipment_equipment_type
foreign key (equipmenttype)
references equipment(equipmenttype),
constraint fk_roomequipment_equipment_roomid
foreign key (roomid)
references room(roomid)
);
其它表可能是這樣的:
-- I prefer using char datatype for predictable number of characters in a field
CREATE TABLE RoomType (
Roomtype nvarchar(2) NOT NULL,
Description nvarchar(20),
Responsibility nvarchar(20),
primary key (Roomtype)
);
-- You could use char(1) instead of nvarchar(1) for type; I'd prefer using int instead
create table Equipment (
Equipmenttype nvarchar(1) NOT NULL,
Description nvarchar(10)
);
-- Added a unique key to assist with good foreign key relationship
alter table Equipment add constraint uk_equipment_equipmenttype unique (equipmenttype);
-- Just use an int or char if roomID length is predictable
Create table Room (
RoomID nvarchar(8),
Capacity numeric(3),
Roomtype(fk,nvarchar(2)
);
alter table room add constraint uk_room_id unique (roomid);
例子是在這裏:http://sqlfiddle.com/#!3/f510b
你想命名你正在創建的表的名字是什麼?你想在那張桌子上放什麼樣的領域?哪些表和字段應該參與外鍵關係? – zedfoxus
我想要創建RoomID和設備類型以將房間與設備關聯,因此新表格將稱爲RoomEquipment。 @zedfoxus – salman12
好的,RoomEquipment是桌子。它將有RoomID和EquipmentType字段。 RoomEquipment的EquipmentType字段將與Equipment的EquipmentType字段相關。 RoomEquipment的RoomID字段與任何東西有關嗎? – zedfoxus