2010-12-22 100 views
3

我有代碼的MySQL錯誤1451MySQL錯誤1451

不能刪除或更新父行:外鍵約束失敗(online_store_adminosa_admin_logs,約束fk_admins_logs外鍵(aid)參考文獻osa_adminsaid) )

這裏的SQL語句:

 

drop table if exists osa_admins; 
create table if not exists osa_admins(
aid int unsigned not null auto_increment, 
uid varchar(50) not null, 
pass char(41) not null, 
erp_id int unsigned not null, 
last_login int unsigned not null, 
is_block tinyint unsigned not null, 
menus varchar(50) not null, 
is_login tinyint unsigned not null, 
ip_login char(15) not null, 

constraint idx_osa_admins primary key using btree(aid) 
); 
insert into osa_admins value 
(NULL, 'root', password('6789'), '0', '0', '0', '*', '0', '127.000.000.001'), 
(NULL, 'ryu', password('6789'), '0', '0', '0', '*', '0', '127.000.000.001'); 

drop table if exists osa_admin_logs; 
create table if not exists osa_admin_logs(
lid bigint unsigned not null, 
aid int unsigned not null, 
dates int unsigned not null, 
logs text not null, 

constraint idx_osa_admin_logs primary key using btree(lid), 
constraint fk_admins_logs foreign key (aid) 
    references osa_admins(aid) 
    match full 
    on update cascade 
    on delete cascade 
); 
insert into osa_admin_logs values 
(NULL, '2', '0', 'some action here'), 
(NULL, '2', '0', 'again, some action here'); 

問題當我使用此語句:

從那裏援助= osa_admins刪除 '2';

我想我已經設置了「刪除級聯」。任何人都知道如何刪除級聯?所以我不需要手動測試osa_admin_logs數據。哦,我使用innodb作爲數據庫引擎(我有默認的MySQL)。

對不起,我問同樣的問題,有答案,只是讓我知道我可以得到我的問題。

謝謝。

回答

8

使用下面的命令來做到這一點:

SET foreign_key_checks = 0; 
DELETE FROM your_table_name WHERE your_condition; 
SET foreign_key_checks = 1; 
+0

這非常危險;它只是告訴計算機忽略這些限制。接受的答案是更好的解決方案。我想你也會發現將查詢的DELETE部分改爲SELECT * FROM,它可能會返回有效的結果。 – Sablefoste 2015-06-22 22:08:43