我正在为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
);

有人可以帮忙吗?

最佳答案

只需重命名两个外键,它应该如下所示工作。
我在本地数据库上使用以下创建表脚本对其进行了测试,并且可以成功创建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
);

10-01 15:39