如何在 sql 中对这些关系进行编码而不会出错

How do I code these relationships in sql without getting am error

附件是我为以下实体关系图创建数据库的尝试。但我不断收到以下错误:

"SQL0538N FOREIGN KEY "ADVISOR_STUDENT" does not conform to the description of the parent key of table or nickname "KISHANPA.STUDENT". SQLSTATE=42830"

对于这 4 个表格:advisor、prereq、teaches、takes。其余的表似乎工作正常。如果有人能指导我正确的方向,我将不胜感激。谢谢

ER Diagram

Schema Diagram

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 
);

docs这样说:

A foreign key references a primary key or a unique key in the same or another table. A foreign key assignment indicates that referential integrity is to be maintained according to the specified referential constraints.

您的 "advisor_student" 引用了 "sid",但这不是主键。您需要包括部门或更改设计。

您的问题出在您的学生密钥上。您必须具有相同的列才能使用 FK 连接表。像这样修改您的 table 学生:

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);

您的主键与多个表的架构图不匹配。

Instructorstudentcourse 在你的 DDL 的主键中都有 dept_name,但是根据 Schema 图,逻辑上也是如此,该字段不应是任何这些表的主键的一部分。另外,section的主键与Schema图不匹配。它应该是 (course_id, sec_id, semester, year) 你那里有很多额外的字段。这将导致 teachestakes.

上的外键约束出现问题

最后,模式和 DDL 中的 time_slot 文件都有我称之为有问题的东西,没有其他输入,主键。在我看来应该只是 time_slot_id,那么您也可以构建从 sectiontime_slot 的外键。