mysql foreign key reference from a composite key

╄→尐↘猪︶ㄣ 提交于 2019-12-13 05:54:00

问题


I am creating an application that will handle and record when a student gets advised by a faculty member at a university and I need an effective way to structure the tables. My problem is coming from a lack of referential integrity, caused by the inability to create a foreign key that will reference a STUDENT on only part of a composite key in ADVISE_HIST which is (STUDENT_ID,DATE_ADVISED)

here are some of the tables

create table STUDENT(
  LNAME varchar(50),
  FNAME varchar(50),
  ID char(9) primary key,
  ASSIGNED_ADVISOR_EMAIL varchar(70),
  foreign key (ASSIGNED_ADVISOR_EMAIL) references DEP_FACULTY(EMAIL) ON DELETE SET NULL,
  IS_ADVISED tinyint(1),
);

create table DEP_FACULTY(
  LNAME varchar(50),
  FNAME varchar(50),
  EMAIL varchar(70) primary key
);

create table ADVISE_HIST(
  STUDENT_ID char(9),
  /*foreign key (STUDENT_ID) references STUDENT(ID),*/
  ACTUAL_ADVISOR_EMAIL char(70) NOT NULL,
  foreign key (ACTUAL_ADVISOR_EMAIL) references DEP_FACULTY(EMAIL),
  DATE_ADVISED date,
  primary key REF_ADVISE_HIST (STUDENT_ID, DATE_ADVISED),
);

My question is, is there a way around not being able to create this key or is there a better structure that I'm not thinking of?


回答1:


MySQL has a lot of restrictions on foreign keys. Among the ones that might be getting in your way . . .

  • Both tables have to use the INNODB engine.
  • "In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. "

This code works in my version (5.1.62).

create table DEP_FACULTY(
  LNAME varchar(50),
  FNAME varchar(50),
  EMAIL varchar(70) primary key
) ENGINE = INNODB;

insert into DEP_FACULTY values ('Gregor', 'Brandich', 'gbrandich@thisdomain.com');

create table STUDENT(
  LNAME varchar(50),
  FNAME varchar(50),
  ID char(9) primary key,
  ASSIGNED_ADVISOR_EMAIL varchar(70),
  foreign key (ASSIGNED_ADVISOR_EMAIL) references DEP_FACULTY(EMAIL) ON DELETE SET NULL,
  IS_ADVISED tinyint(1)
) ENGINE = INNODB;

insert into STUDENT values ('Charmaine', 'DePeletier', 'cmd', 'gbrandich@thisdomain.com', 1);

create table ADVISE_HIST(
  STUDENT_ID char(9),
  foreign key (STUDENT_ID) references STUDENT(ID),
  ACTUAL_ADVISOR_EMAIL char(70) NOT NULL,
  foreign key (ACTUAL_ADVISOR_EMAIL) references DEP_FACULTY(EMAIL),
  DATE_ADVISED date,
  primary key REF_ADVISE_HIST (STUDENT_ID, DATE_ADVISED)
) ENGINE = INNODB;

insert into ADVISE_HIST values ('cmd', 'gbrandich@thisdomain.com', CURRENT_DATE);
insert into ADVISE_HIST values ('ctd', 'gbrandich@thisdomain.com', CURRENT_DATE);

Of those last two inserts, the first works. The second should throw a foreign key constraint error.



来源:https://stackoverflow.com/questions/10321295/mysql-foreign-key-reference-from-a-composite-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!