我正在尝试创建一个引用时隙表的部分表。属性的详细信息如图所示。如何声明外键约束?红线是我收到此错误的地方
create table Section
(
course_id nvarchar(8) foreign key references Course(course_id) on delete cascade,
sec_id nvarchar(8),
semester nvarchar(6) check(semester in ('Spring','Fall','Summer','Winter')),
year_ numeric(4,0) check(1700 < year_ and year_ < 2100),
building nvarchar(15),
room_number nvarchar(7),
time_slot_id nvarchar(4),
primary key(course_id,sec_id,semester,year_),
--Foreign key =>(Section to Classroom)
constraint FK_Section_to_Classroom
foreign key(building,room_number)
references Classroom(building,room_number)
on delete set null,
--Foreign key =>(Section to TimeSlot)
constraint FK_Section_to_TimeSlot
foreign key(time_slot_id)
references TimeSlot(time_slot_id,day_of_week,start_time)
on delete set null
);
create table TimeSlot
(
time_slot_id nvarchar(4),
day_of_week nvarchar(1) check(day_of_week in ('M', 'T', 'W', 'R', 'F', 'S', 'U')),
start_time time,
end_time time,
primary key(time_slot_id,day_of_week,start_time)
);
节表外键是一个单一属性,我想引用时隙表的主键,其中是一组属性。还有其他方法来声明外键约束吗?
您遇到的错误“外键中引用列的数量与引用列的数量不同”是由于您在外键约束中引用了多个列 (TimeSlot(time_slot_id,day_of_week,start_time)) ,但您只指定引用表中的单个列 (time_slot_id)。
要解决此问题,您需要确保外键中的列数与引用键中的列数匹配。
如果您想保持部分表和时隙表之间的引用完整性,您可能需要重新考虑您的表结构。由于您的 TimeSlot 表具有复合主键(time_slot_id、day_of_week、start_time),因此无法直接使用部分表中的单个列来引用它。