MySql外键约束问题

时间:2017-04-01 01:15:30

标签: mysql sql database

我正在尝试开发一个大学数据库,并且我遇到了几个抛出约束错误的表。我尝试了各种变通方法: 1.检查我的状态。所有表格都是INNODB。 2.关于更新删除参数(虽然我不确定我是否正确执行此操作)。 3.检查NULL引用和数据类型。引用的外键具有与作为引用的表中的主键相同的数据类型。     这是我的问题:

CREATE TABLE Faculty (
FacNo         CHAR(11)      NOT NULL,
FacFirstName  VARCHAR(30)   NOT NULL,
 FacLastName   VARCHAR(30)   NOT NULL,
FacCity       VARCHAR(30)   NOT NULL,
FacState      CHAR(2)       NOT NULL,
FacDept       CHAR(6)       NULL,
FacRank       CHAR(4)       NULL,
FacSalary     DECIMAL(10,2) NULL,
FacSupervisor CHAR(11)      NOT NULL,
FacHireDate   DATETIME      NULL,
FacZipCode    CHAR(10)      NOT NULL,
CONSTRAINT FacultyPK PRIMARY KEY (FacNo),
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty
ON DELETE NO ACTION 
ON UPDATE NO ACTION )engine = innodb;

这是提供参考的OFFERING表:

CREATE TABLE Offering (
OfferNo       INTEGER       NOT NULL,
CourseNo      CHAR(6)       NOT NULL,
OffTerm       CHAR(6)       NOT NULL,
OffYear       INTEGER       NOT NULL,
OffLocation   VARCHAR(30)   NULL,
OffTime       VARCHAR(10)   NULL,
FacNo         CHAR(11)      NOT NULL,
OffDays       CHAR(4)       NULL,
CONSTRAINT OfferingPK PRIMARY KEY (OfferNo),
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course,
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty  )ENGINE =     INNODB;

1 个答案:

答案 0 :(得分:0)

您的外键引用缺少列:

CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty(Facno)

CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course(CourseNo),

CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty(FacNo)

Here是一个SQL小提琴。它没有CourseNo外键,因为问题中没有定义。