2

is it possible to have a foreign key with two reference tables?

here's the script and we're having a problem inserting into this table

CREATE TABLE Class(
    idNumber varchar(30) not null,
    day varchar(10) not null,
    time varchar(20) not null,
    foreign key(idNumber) REFERENCES ThesisMember(idNumber),
    foreign key(idNumber) REFERENCES Faculty(idNumber),
    PRIMARY KEY(idNumber,day,time)
);

CONSTRAINT `class_ibfk_1`
    FOREIGN KEY (`idNumber`) REFERENCES `thesismember` (`idNumber`)) 

SQL Statement:

INSERT INTO `thesis`.`class`
    (`idNumber`, `day`, `time`) VALUES ('9990', 'F', '0940-1110')

the Faculty table has the "9990" idNumber.

Blizzer
  • 260
  • 4
  • 22
Kopaka
  • 57
  • 5

1 Answers1

1

The FK constraint requires that every instance of the field 'idNumber' occur in ALL referenced tables. You state that only one of the referenced tables has the value to be inserted, so of course an error occurs on the INSERT attempt.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
  • No; it is possible PROVIDED that the idNumber is always going to occur in both tables. You should have a master table owning the PK idNumber, with ThesisMember and FacultyMember being detail tables off of it. An additional idType field would identify whether an idOwner was a ThesisMember or a FacutlyMember. – Pieter Geerkens Mar 06 '13 at 08:48