SQL不允许将数据添加到外键中[重复]

Posted

技术标签:

【中文标题】SQL不允许将数据添加到外键中[重复]【英文标题】:SQL not allowing to add data into foreign keys [duplicate] 【发布时间】:2021-12-10 05:49:14 【问题描述】:

我在 mysql 中遇到了一个错误,它不允许我输入外键数据。这是我的数据:

 create table Location(
LocationID char(4) primary key,
LocationName char(20),
LocationState char(3),
LocationPostCode char(4),
StartLoc char(3),
EndLoc char(3)
);
drop table Location;
insert into Location values ('L10','Norwood','TAS','7250','L31','L30');
insert into Location values ('L11','Cressy','TAS','7586','L33','L32');
insert into Location values ('L12','Launceston','TAS','7907','L35','L34');
insert into Location values ('L13','Epping Forest','TAS','7354','L37','L36');
insert into Location values ('L14','Avoca','TAS','7812','L39','L38');
insert into Location values ('L15','Royal George','TAS','7009','L41','L40');
insert into Location values ('L16','Westbury','TAS','7893','L43','L42');

create table TripSchedule(
ScheduleID char(3),
foreign key(StartLoc) references Location(StartLoc),
foreign key(EndLoc) references Location(EndLoc),
foreign key(RequestID) references BookingReq(RequestID),
foreign key(TruckVINNum) references Allocation(TruckVINNum),
foreign key(TransportID) references Allocation(TransportID),
foreign key(StaffID) references Staff(StaffID),
TripStart char(40),
TripEnd char(40)
);

drop table TripSchedule;
insert into TripSchedule values('S23',L11','L10', 'R101', 'VO20','T1','S1''6th of December 2020-1:30pm','7th of December 2021-1:30 pm');
insert into TripSchedule values('S24', 'L13','L12', 'R102', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');
insert into TripSchedule values('S25', 'L15','L14', 'R103', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');
insert into TripSchedule values('S26', 'L17','L16', 'R104', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');
insert into TripSchedule values('S27', 'L19','L18', 'R105', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');
insert into TripSchedule values('S28', 'L21','L20', 'R101', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');
insert into TripSchedule values('S29', 'L23','L22', 'R106', 'VO20','T1','S1','6th of December 2020-1:30pm','7th of December, 2021-1:30 pm');

我遇到的问题有点奇怪,它与 Location 和 TripSchedule 表之间的关系有关。 SQL 将允许输入 TripSchedule 表的前三行插入值,但其余插入值给出错误:“错误代码:1452。无法添加或更新子行:外键约束失败(评估.tripschedule, CONSTRAINT tripschedule_ibfk_1 FOREIGN KEY (StartLoc) REFERENCES location (LocationID))"。

我尝试将 StartLoc 和 EndLoc 引用为 Location 表中的主键,并且还尝试从 Location 表中完全删除 StartLoc 和 EndLoc 但仍然遇到同样的问题。有什么解决办法吗?

【问题讨论】:

你创建的表不一致,没有包含所有信息;提问时请尽量提供实际的表结构,如show create table yourtablename所示 【参考方案1】:

您的外键约束要求位置存在以便将它们指定为 StartLoc。您的第四到第七个插入将 StartLoc 指定为 L17、L19、L21 和 L23,它们都不是存在的 LocationID。

在插入引用它们的 TripSchedule 记录之前创建位置记录,或者删除您的外键约束:

alter table TripSchedule drop constraint tripschedule_ibfk_1;

【讨论】:

以上是关于SQL不允许将数据添加到外键中[重复]的主要内容,如果未能解决你的问题,请参考以下文章

WPF XAML - 将数据网格列绑定到外键(数据集)

在 My SQL 中不断收到外键约束失败消息

SQL中啥叫主键,啥是外键,有啥关系

请问SQL server 中的主键和外键的作用

主键和外键的作用

如何像管理面板一样在 Django 的外键中添加新项目?