Verify that SET FOREIGN KEY CHECKS is set to = 1 - mysql

I would like to DROP and create back the same table in using phpmyadmin.
DROP TABLE IF EXISTS `product_entity`;
CREATE TABLE `product_entity` ( ...... );
I get error message
Cannot delete or update a parent row: a foreign key constraint fails
Then I set
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `product_entity`;
SET FOREIGN_KEY_CHECKS=1;
CREATE TABLE `product_entity` ( ...... );
My question is how to verify that SET FOREIGN KEY CHECKS is set to = 1?
Thanks.

Try this in console
SELECT ##foreign_key_checks;
See Demo

Try this in console
show variables like 'fo%'
http://sqlfiddle.com/#!2/d41d8/29738

Related

Trying to disable foreign key checks, but unsuccessful

I am trying to temporarily disable foreign key checks, but have not been successful.
```
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `menu_items`;
CREATE TABLE `menu_items` (
...
SET FOREIGN_KEY_CHECKS = 1;
```
It keeps telling me that I cannot delete or update a parent row. Any help would be appreciated!
To disable foreign key constraints when you want to truncate a table:
Use FOREIGN_KEY_CHECKS
SET FOREIGN_KEY_CHECKS=0;
Or you can use DISABLE KEYS:
ALTER TABLE table_name DISABLE KEYS;

cannot update foreign key in different session mysql

I have a problem updating a field in a table when i'm in a new session. I have this two tables:
I want to update the idusuario from the pedidos table to reference a different usuario.
I have tried inserting the idusuario fk as null but it neither works
If I execute the update in the same session where I have created the tables there is no problem. See image.
But if I try to execute the same query in another session it crashes with the next error code: (also tried on command line console)
You should drop your foreign key , update , and then re add it :
ALTER TABLE pedidos
DROP FOREIGN KEY FK_name;
UPDATE pedidos set idusuario = 4 where id_pedidos = 2;
ALTER TABLE pedidos
ADD CONSTRAINT FK_name FOREIGN KEY (IdUsuario) REFERENCES SecondTable(idPedidos);

Unable to insert row after MySQL server restart (Cannot add or update a child row: a foreign key constraint fails)

I have a simple DB with two tables (Games, Screen) one related to another through a foreign key (a Game can have many Screens, a Screen 'belongs to' one Game).
When I try to insert a new row in the Screen table everything works as expected. I'm using this query to add a new Game:
INSERT INTO Game ( title ) VALUES ( "Game 1" );
And this one to add new Screens:
INSERT INTO Screen ( title, game_id ) VALUES ( "screen 3", 1 );
I can insert as many rows as I want with this query with no problem. However, if I just restart the MySQL server (I'm using MAMP, http://www.mamp.info/en/index.html) and execute the same query again, I get this error:
Cannot add or update a child row: a foreign key constraint fails (`mydb`.`screen`, CONSTRAINT `fk_Screen_Game` FOREIGN KEY (`Game_id`) REFERENCES `Game` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I have no clue about what the problem can be, since this only happens after server restart.
From the error message it looks as if the Game table didn't have a row with id = 1, but it does...
Here's the script I use to create the database:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Game`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Game` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Game` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Screen`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Screen` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Screen` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NULL,
`Game_id` INT NOT NULL,
PRIMARY KEY (`id`, `Game_id`),
INDEX `fk_Screen_Game_idx` (`Game_id` ASC),
CONSTRAINT `fk_Screen_Game`
FOREIGN KEY (`Game_id`)
REFERENCES `mydb`.`Game` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Maybe I haven't set my table relationship right?
Also, this thread seems very similar to mine: AFTER MySql restart : #1452 - Cannot add or update a child row: a foreign key constraint fails but I tried executing
FLUSH TABLES;
with no luck...
Thanks in advance.
Mihai: Yes, dropping the db solves it, but I guess I shouldn't need to do that. Right now my DB is mostly empty (just testing stuff), but in the future it will contain data that I don't want to loose...
Gim: yes, there's a Game with that id.
Nick Tsarev: Will take note of that, thanks. However I can't test your query anymore, please read below:
SOLVED
Finally I found this post:
Weird ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
which solved the problem. Looks like my local MySQL version had a bug causing the problem. I just upgraded to 5.5.25 and looks like it works correctly now.
Thanks a lot everyone.

Trying to Run a stored procedure in mySQL and it fails on a foreign key constraint

I wrote a simple Stored Procedure with a while loop that's not working.
Here's some data for you:
the parent table is data_client_id. The tables are all innoDB. The primary key is ID. All other tables in the schema with the data_ prefix have this (data_client_id.id) column as a foreign key.
I generated some dummy data and inserted it into the data_client_id table which auto-increments the primary key. The rows generated were 33 through 132. My intention was to write one stored procedure that would create rows with matching values in the id column for all of the other data_ tables.
Here is my code:
mysql> DELIMITER //
mysql> CREATE PROCEDURE this()
-> BEGIN
-> DECLARE v1 INT;
-> SET v1 = 33;
-> WHILE v1 < 132 DO
-> INSERT INTO data_banking (id) VALUES(v1);
-> SET v1 = v1+1;
-> END WHILE;
-> END//
Query OK, 0 rows affected (0.00 sec)
Then when I CALL the this(); Procedure I get the following error:
mysql> call this();
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`oms_clients`.`data_banking`, CONSTRAINT `data_banking_FK` FOREIGN KEY (`id`) REFERENCES `data_client_id` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I tried just running standalone INSERTs into the data_ tables like this:
INSERT into data_banking (id) VALUES (34);
and it worked setting all the columns with defaults to NULL or their respective SET defaults...
There's nothing wrong with your stored procedure.
The problem is that you can't insert a run into data_banking unless the SAME id also exists in data_client_id. Each id, 33 through 132.
To fix the problem, remove the constraint, or make sure the foreign key dependency is resolved.
Here's a good link with some useful tips:
Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails

Bogus foreign key constraint fail

I get this error message:
ERROR 1217 (23000) at line 40: Cannot
delete or update a parent row: a
foreign key constraint fails
... when I try to drop a table:
DROP TABLE IF EXISTS `area`;
... defined like this:
CREATE TABLE `area` (
`area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
`nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
The funny thing is that I already dropped all other tables in the schema that have foreign keys against area. Actually, the database is empty except for the area table.
How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?
(I can even run a RENAME TABLE area TO something_else command :-?)
On demand, now as an answer...
When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (bugs.mysql.com/bug.php?id=8280), making it neccessary to write all the drop statements in one query, eg.
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE my_first_table_to_drop;
DROP TABLE my_second_table_to_drop;
SET FOREIGN_KEY_CHECKS=1;
Where the SET FOREIGN_KEY_CHECKS=1 serves as an extra security measure...
Two possibilities:
There is a table within another schema ("database" in mysql terminology) which has a FK reference
The innodb internal data dictionary is out of sync with the mysql one.
You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.
If it turns out to be the latter case, I'd dump and restore the whole server if you can.
MySQL 5.1 and above will give you the name of the table with the FK in the error message.
Disable foreign key checking
SET FOREIGN_KEY_CHECKS=0
from this blog:
You can temporarily disable foreign key checks:
SET FOREIGN_KEY_CHECKS=0;
Just be sure to restore them once you’re done messing around:
SET FOREIGN_KEY_CHECKS=1;
hopefully its work
SET foreign_key_checks = 0;
DROP TABLE table name;
SET foreign_key_checks = 1;
On Rails, one can do the following using the rails console:
connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")
Maybe you received an error when working with this table before. You can rename the table and try to remove it again.
ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;
i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done
Cannot delete or update a parent row: a foreign key constraint fails (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds##5A6BD60 FOREIGN KEY (user_id) REFERENCES user (id))
What i did in two simple steps . first i delete the child row in child table like
mysql> delete from table2 where role_id = 2 && user_id =20;
Query OK, 1 row affected (0.10 sec)
and second step as deleting the parent
delete from table1 where id = 20;
Query OK, 1 row affected (0.12 sec)
By this i solve the Problem which means Delete Child then Delete parent
i Hope You got it. :)