gpt4 book ai didi

php - mysql不能添加2个外键

转载 作者:可可西里 更新时间:2023-11-01 08:04:59 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com