Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql无法添加2个外键_Php_Mysql_Sql_Windows - Fatal编程技术网

Php mysql无法添加2个外键

Php mysql无法添加2个外键,php,mysql,sql,windows,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

我正在为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
);
上面的两个表工作正常,当我将下面的表链接到上面的两个表时,使用两个外键会出现问题

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
);

只需重命名两个外键,其工作原理如下。。 我在本地数据库上使用下面的CREATETABLE脚本对其进行了测试,我可以成功地创建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
);

您收到了什么错误消息?非常感谢您的帮助!这个社区太棒了!非常感谢你,它成功了!这个社区太棒了!