我對SQL仍然比較陌生。我正在更新數據庫,並且遇到了此消息。問題是,我已經執行了此插入,但不得不刪除它,因爲我三次輸入相同的地址,而不是一次。MySQL工作臺:錯誤代碼1452.無法添加或更新子行:外鍵約束失敗
任何人可以幫助我,我不明白什麼是錯的:
> 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'))
;
我不斷收到此消息:
> 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
當它沒有找到出現這種錯誤在主鍵表中提供了外鍵。您的表'liegenschaft'中的插入查詢中的一個嵌套選擇查詢必須爲null或不匹配。 – ARr0w