Nin*_*inj 10 mariadb foreign-key
在 MySql 上,为了在由于外键(仅限 InnoDB)而通常不可能的情况下截断表或删除行,我们使用以下命令:
SET FOREIGN_KEY_CHECKS=0;
在 MariaDB 上,虽然此命令被接受,但它什么也不做。
该文件说我必须改为运行以下命令:
在每张表的基础上:
ALTER TABLE `...` DISABLE KEYS;
Run Code Online (Sandbox Code Playgroud)
或者,在全球范围内:
SET @@session.unique_checks = 0;
SET @@session.foreign_key_checks = 0;
Run Code Online (Sandbox Code Playgroud)
所以我尝试运行这个脚本:
SET FOREIGN_KEY_CHECKS=0;
SET @@session.unique_checks = 0;
SET @@session.foreign_key_checks = 0;
ALTER TABLE `country` DISABLE KEYS;
DELETE FROM `country` WHERE 1;
Run Code Online (Sandbox Code Playgroud)
这导致我:
无法删除或更新父行:外键约束失败 (
database.region, CONSTRAINTFK_F62F176F92F3E70FOREIGN KEY (country_id) REFERENCEScountry(id))
使用TRUNCATE具有相同的效果。
如果您对我可能错过的内容有任何想法,请提前致谢。因为文档和现有问题在这里没有多大帮助。
Len*_*art 13
它应该与:
SET FOREIGN_KEY_CHECKS=0
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
create table parent (x int not null primary key) engine = innodb;
create table child (x int not null primary key, constraint aaa foreign key (x) references parent (x) on delete restrict) engine = innodb;
insert into parent (x) values (1),(2);
-- test if f.k is active
insert into child (x) values (1),(3);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails ("test"."child", CONSTRAINT "aaa" FOREIGN KEY ("x") REFERENCES "parent" ("x"))
insert into child (x) values (1);
SET FOREIGN_KEY_CHECKS=0;
delete from parent;
select * from parent;
Empty set (0.00 sec)
select * from child;
+---+
| x |
+---+
| 1 |
+---+
select @@version
-> ;
+-----------------+
| @@version |
+-----------------+
| 10.1.16-MariaDB |
+-----------------+
Run Code Online (Sandbox Code Playgroud)
所以问题不在于 MariaDB,而在于 PhpMyAdmin。它在您可以执行 SQL 的页面上有一个复选框,它会覆盖SET FOREIGN_KEY_CHECKS=. 如果要禁用外键验证,则必须取消选中它。
| 归档时间: |
|
| 查看次数: |
18734 次 |
| 最近记录: |