patternModerate
MariaDB: disable foreign key checks
Viewed 0 times
checksdisablemariadbforeignkey
Problem
On MySql, in order to truncate a table or delete rows when it's normally impossible because of foreign keys (InnoDB only), we use this command:
On MariaDB, while this command is accepted, it does nothing.
The documentation says I have to run these commands instead:
On a per-table basis:
or, globally:
So I tried to run this script:
And that leads me to:
Cannot delete or update a parent row: a foreign key constraint fails (
Using
If you have any idea on what I may have missed, thank you by advance. Because the documentation and the existing questions weren't much of help here.
SET FOREIGN_KEY_CHECKS=0;On MariaDB, while this command is accepted, it does nothing.
The documentation says I have to run these commands instead:
On a per-table basis:
ALTER TABLE `...` DISABLE KEYS;or, globally:
SET @@session.unique_checks = 0;
SET @@session.foreign_key_checks = 0;So I tried to run this script:
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;And that leads me to:
Cannot delete or update a parent row: a foreign key constraint fails (
database.region, CONSTRAINT FK_F62F176F92F3E70 FOREIGN KEY (country_id) REFERENCES country (id))Using
TRUNCATE has the same effect.If you have any idea on what I may have missed, thank you by advance. Because the documentation and the existing questions weren't much of help here.
Solution
It should work with:
I tried the following:
So the problem is not with MariaDB, but with PhpMyAdmin. It has a checkbox on the pages where you can execute SQL, that overrides
SET FOREIGN_KEY_CHECKS=0I tried the following:
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 |
+-----------------+So the problem is not with MariaDB, but with PhpMyAdmin. It has a checkbox on the pages where you can execute SQL, that overrides
SET FOREIGN_KEY_CHECKS=. One must uncheck it if one wants to disable foreign key validation.Code Snippets
SET FOREIGN_KEY_CHECKS=0create 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 |
+-----------------+Context
StackExchange Database Administrators Q#146349, answer score: 17
Revisions (0)
No revisions yet.