php - mysql不能添加2个外键

标签 php mysql sql windows

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

有人能帮忙吗?

最佳答案

外键需要有不同的约束名称。试试这个:

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 fk_AppointmentDate1_AdviserId foreign key(AdviserID)
        references Adviser(AdviserID)
            on delete no action 
            on update no action,
        constraint fk_AppointmentDate1_StudentId foreign key(StudentID)
        references Student(StudentID)
            on delete no action 
            on update no action
);

关于php - mysql不能添加2个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216184/

相关文章:

php - 条形码无法读取mysql中的行

php - php 变量中的斜杠在 javascript 中未正确解析

mysql 到 postgresql 查询转换

mysql - 重置/压缩mysql AI ID字段?

php - mysqlnd 缺少驱动程序的解决方法

php - 使用 MySQL 创建的菜单仅适用于网站内部,不适用于外部

python - 在 MySql 和 python 中将变量值插入变量列

php - 如何在 Phalcon 中正确初始化数据库连接

SQLZOO SELECT 来自诺贝尔 #14

mysql - SQL查询以选择具有2列相等值的行