附件是我尝试为以下实体关系图创建数据库。但我不断收到以下错误:
“SQL0538N FOREIGN KEY”ADVISOR_STUDENT“不符合 表或昵称“KISHANPA.STUDENT”的父键的描述。 SQLSTATE = 42830"
对于这4个表:advisor,prereq,teaches,takes。其余的表似乎工作正常。如果有人能指导我正确的方向,我将不胜感激。感谢
create table department (
dept_name varchar(30) not null,
building varchar(30),
budget numeric(7,2),
constraint department_key primary key (dept_name)
);
create table instructor (
iid char(9) not null,
name varchar(30) not null,
dept_name varchar(30) not null,
salary numeric(6,2),
constraint instructor_key primary key (iid, dept_name),
constraint instructor_dept foreign key(dept_name)
references department on delete no action
);
create table student (
sid char(9) not null,
name varchar(30) not null,
tot_cred smallint,
dept_name varchar(30) not null,
constraint student_key primary key (sid, dept_name),
constraint student_dept foreign key(dept_name)
references department on delete no action
);
create table course (
course_id char(8) not null,
title varchar(30) not null,
dept_name varchar(30) not null,
credits int not null,
constraint course_key primary key (course_id, dept_name),
constraint course_dept foreign key(dept_name)
references department on delete no action
);
create table advisor (
sid char(9) not null,
iid char(9) not null,
constraint advisor_key primary key (sid, iid),
constraint advisor_student foreign key(sid)
references student on delete no action,
constraint advisor_instructor foreign key (iid)
references instructor on delete no action
);
create table prereq (
course_id char(8) not null,
prereq_id char(8),
constraint prereq_key primary key (course_id),
constraint prereq_course foreign key(course_id)
references course on delete no action,
constraint prereq_precourse foreign key(prereq_id)
references course on delete no action
);
create table classroom (
building varchar(30) not null,
room_number varchar(10) not null,
capicity integer,
constraint classroom_key primary key (building, room_number)
);
create table time_slot (
time_slot_id varchar(10) not null,
day varchar(10) not null,
start_time time not null,
end_time time,
constraint time_slot_key primary key (time_slot_id, day, start_time)
);
create table section (
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric (4,0) not null,
building varchar(30) not null,
room_number varchar(10) not null,
time_slot_id varchar(10) not null,
constraint section_key primary key(course_id, sec_id, year,
building, room_number, time_slot_id),
constraint section_classroom foreign key(building, room_number)
references classroom on delete no action
);
create table teaches (
iid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
constraint teaches_key primary key (iid, course_id, sec_id,
semester, year),
constraint section_instrictor foreign key(iid)
references instructor on delete no action,
constraint teaches_section foreign key(course_id, sec_id, semester, year)
references section on delete no action
);
create table takes (
sid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
grade real,
constraint takes_key primary key (sid, course_id, sec_id,
semester, year),
constraint student_takes foreign key(sid)
references student on delete cascade,
constraint takes_section foreign key(course_id, sec_id,
semester, year) references section on delete cascade
);
答案 0 :(得分:2)
docs这样说:
外键引用相同或中的主键或唯一键 另一张桌子。外键分配表示引用 根据指定的参考保持完整性 约束
您的“advisor_student”引用“sid”,但这不是主键。您需要包括部门或更改设计。
答案 1 :(得分:2)
你的问题在于学生的关键。您必须使用相同的列才能加入FK标签。像这样修改你的表学生:
create table student (sid char(9) not null, name varchar(30) not null, tot_cred smallint, dept_name varchar(30) not null, constraint student_key primary key (sid), constraint student_dept foreign key(dept_name) references department on delete no action);
答案 2 :(得分:0)
您的主键与多个表的架构图不匹配。
Instructor
,student
和course
在您的DDL中的主键中都有dept_name
,但根据Schema图和逻辑上,此字段不应该是任何这些表的主键的一部分。此外,section
的主键与Schema图不匹配。它应该是(course_id, sec_id, semester, year)
你有很多额外的字段。这将导致teaches
和takes
上的外键约束出现问题。
最后,模式和DDL中的time_slot
文件都有我可以称之为有问题的文件,没有其他输入,主键。我应该只考虑time_slot_id
,那么您也可以构建从section
到time_slot
的外键。