简体   繁体   English

MySQL Workbench:错误代码1452。无法添加或更新子行:外键约束失败

[英]MySQL Workbench: Error Code 1452. Cannot add or update a child row: a foreign key constraint fails

I'm still fairly new to SQL. 我对SQL还是很陌生。 I'm updating a DB and I came across this message. 我正在更新数据库,并且遇到了此消息。 The problem is, I've already executed this insert before but had to delete it due to me entering the same address 3 times instead of once. 问题是,我之前已经执行过此插入操作,但是由于我输入相同的地址3次而不是一次,所以不得不删除它。

Can anybody help me, I don't understand what is wrong: 谁能帮我,我不明白哪里出了问题:

> insert into ort  
    (plz, name) values    
    ('4900', 'Langenthal')  
;

>insert into adresse  
    (strasse, strassennr, ortID) values  
    ('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'))  
;
>
insert into liegenschaft  
    (liegenschafttypid, adressid) values  
    ((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'), (select oid from ort where name = 'Langenthal' and plz = '4900'))  
;

I keep on getting this message: 我不断收到此消息:

> 0 16  14:09:25    insert into liegenschaft   (liegenschafttypid, adressid) values
     ((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'),   (select oid from ort where name = 'Langenthal' and plz = '4900'))    Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`parking`.`liegenschaft`, CONSTRAINT `FK_adresse` FOREIGN KEY (`adressID`) REFERENCES `adresse` (`AID`))    0.015 sec

The column adressid in liegenschaft shall have the key adressID and not oid. liegenschaft中的adressid列应具有键adressID而不是oid。

Try (assume that there just one entry for each address) 试试(假设每个地址只有一个条目)

insert into liegenschaft (liegenschafttypid, adressid) values ((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'), (select adressID from ort where name = 'Langenthal' and plz = '4900')) 插入liegenschaft(liegenschafttypid,adressid)值((从liegenschaft_typ中选择ltypid,其中名称='Wohnhaus / Firma'),(从ort中选择adressID,其中名称='Langenthal'和plz ='4900'))

You don't have entry in column adresse.AID for liegenschaft.adressid that you want to insert. 您想要插入的adresse.AIDliegenschaft.adressid列中没有条目。

You either specified wrong column in foreign key, insert or you forgot to insert data into that column. 您要么在外键中指定了错误的列,然后插入,要么忘记将数据插入到该列中。

You need to do one of those: 您需要执行以下一项操作:

insert into adresse
(strasse, strassennr, AID) values
('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'));

or 要么

insert into adresse
(strasse, strassennr, ortID, AID) values
('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'), (select oid from ort where name = 'Langenthal' and plz='4900'));

or alter that foreign key to point at ortID instead AID 或更改该外键以指向ortID而不是AID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL 错误代码:1452。无法添加或更新子行:外键约束失败 - MySQL Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails Mysql:错误代码:1452。无法添加或更新子行:外键约束失败 - Mysql: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails 错误代码:1452。无法添加或更新子行:外键约束失败 - Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails MySql Workbench 错误 1452 无法添加或更新子行 外键约束失败 - MySql Workbench error 1452 cannot add or update a child row a foreign key constraint fails 错误代码:1452无法添加或更新外键约束失败的子行mysql - Error Code: 1452 cannot add or update a child row a foreign key constraint fails mysql MySQL工作台。 错误1452:无法添加或更新子行:外键约束失败。 手术失败 - MySQL Workbench. ERROR 1452: Cannot add or update a child row: a foreign key constraint fails. Operation failed 错误代码:1452。无法添加或更新子行 - Error Code: 1452. Cannot add or update a child row Mysql 错误 1452 - 无法添加或更新子行:外键约束失败 - Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行,外键约束失败,mysql错误号:1452 - cannot add or update a child row a foreign key constraint fails mysql Error Number: 1452 MYSQL错误1452(23000):无法添加或更新子行:外键约束失败另一个问题 - MYSQL ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails Another issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM