mysql 不能添加 2 外键

标签: sql MySQL PHP windows
发布时间: 2016/10/23 4:59:46
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

Im mySQL 和在第二个外键约束其停止汇编编写用于关联表的脚本,有谁知道什么可能是错误?请不胜感激 !

create table Adviser(
    AdviserID integer not null,
    LastName char(25) not null,
    FirstName char(25) not null,
    AdviserEmail varchar(100) not null,
    OfficePhoneNumber char(12) not null,
    constraint Adviser_pk primary key(AdviserID),
    constraint Adviser_fk foreign key(OfficePhoneNumber)
        references Department(OfficePhoneNumber)
            on delete no action 
            on update no action
);

create table Student(
    StudentID integer not null,
    LastName char(25) not null,
    FirstName char(25) not null,
    StudentEmail varchar(100) not null,
    EnrollmentDate date not null,
    GradDate date not null,
    Degree char(25) not null,
    DormPhoneNumber char(12) not null,
    constraint Student_pk primary key(StudentID),
    constraint Student_fk foreign key(DormPhoneNumber)
        references Dorm(DormPhoneNumber)
            on delete no action 
            on update no action
);

做工精细,上面的两个表我犯下了两条链路表的时候有的错误有 2 的外键

create table AppointmentDate1(
    AdviserID integer not null,
    StudentID integer not null,
    StudentAppointmentDate date not null,
    StudentEndDate date not null,
    constraint AppointmentDate1_pk primary key(AdviserID, StudentID),
    constraint AppointmentDate1_fk foreign key(AdviserID)
        references Adviser(AdviserID)
            on delete no action 
            on update no action,
        constraint AppointmentDate1_fk foreign key(StudentID)
        references Student(StudentID)
            on delete no action 
            on update no action
);

谁能帮?

解决方法 1:

只是重命名两个外键,它应该只是作为下面工作。 我是在我本地的数据库上使用以下创建表脚本测试和我可以成功地创建 AppointmentDate1 表。

create table AppointmentDate1(
    AdviserID integer not null,
    StudentID integer not null,
    StudentAppointmentDate date not null,
    StudentEndDate date not null,
    constraint AppointmentDate1_pk primary key(AdviserID, StudentID),
    constraint AdviserId1_fk foreign key(AdviserID)
        references Adviser(AdviserID)
            on delete no action 
            on update no action,
        constraint StudentId1_fk foreign key(StudentID)
        references Student(StudentID)
            on delete no action 
            on update no action
);
官方微信
官方QQ群
31647020