Script to Delete Data from Mysql Tables with Foreign Key Constraints - mysql

I have list of tables (Mysql) where some of them have reference.
I want to to generate delete script for all the tables based on time column.
NOTE: we can not user delete Cascade I can not disable the keys as it is production system.

You can temporarily disable check reference
SET foreign_key_checks = 0;

Related

SQLAlchemy configration for MySQL's "SET foreign_key_checks = 0"

I am using SQLAlchemy with Flask to create database tables - every table has at least one foreign key - it works with sqlite but not MySQL - I get foreign key integrity error when creating the tables in MySQL (the parent table is not created when creating the child table). I use "SET foreign_key_checks = 0" to solve the problem but that does not work with sqlite. Is there a way to configure SQLAlchemy to ignore foreign key checks?
if you are user mysql, you can connect to mysql and use SET GLOBAL FOREIGN_KEY_CHECKS = 0; delete the db table you want, and again SET GLOBAL FOREIGN_KEY_CHECKS = 1;. This value verifies foreign relationships in the db tables.
First of all, why would you want to ignore foreign key constraints? When you define your foreign key you can pass a string with the foreign table and column names, and those will be resolved correctly even if the foreign table hasn't been created yet.
But in any case, if you want to have foreign keys that are not enforced, just don't define your foreign key columns as foreign keys, define them as simple columns and manage the foreign key dependency yourself. Not a good idea, in my opinion, but it can be done.

How to disable all foreign keys in phpMyAdmin when exporting table data?

I exported all tables of my database. I opened the generated file then I saw that phpMyAdmin sorts the queries by table_name. So there is potential foreign key issues because master table is created/inserted after detail table! So how to disable foreign key checks when exporting table data with phpMyAdmin, and how to re-enable them at the end of the script?
Just disable foreign key checks before and re-enable them after you execute your script:
SET foreign_key_checks = 0;
-- run some queries
SET foreign_key_checks = 1;
From the Export tab, select the "Custom" export method.
In the "Format-specific options:" area, look for and check "Disable foreign key checks".
It does the same thing Misa Lazovic suggested to do, but since you're using phpMyAdmin to do the export this is the graphical way of adding those lines.
I also faced the same issue for importing the database in server. And tried all answers above but couldn't figure out. This tutorial fixed my issue and made me able to import my DB in phpmyadmin. Tutorial
Hope this one helped!
When creating the foreign key you should add this option :
ON DELETE CASCADE Or
On delete set null
So when you will delete the foreign key, there will be no PB.

On delete restrict mysql is not working

I want to prevent deleting from parent table when he has children in other tables that.
I make like this
ALTER TABLE constant_det_tb
ADD CONSTRAINT fk_idparent
FOREIGN KEY (idparent)
REFERENCES constant_tb(id) ON DELETE RESTRICT
When I delete from parent constant_tb table, it delete the rows even the table has reference to another tables and it has records reference to it.
Make sure you have InnoDB as storage engine for all affected tables.
Check this (if not already) : http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
ON DELETE RESTRICT
reference option is all you need to achieve this.
Checking of foreign keys could be disabled for current session.
It can be enabled with
SET FOREIGN_KEY_CHECKS = 1;

Adding constraints in phpMyAdmin

I feel like I'm being stupid, but I can't find anywhere on the phpMyAdmin interface to add constraints to foreign keys e.g. CASCADE ON DELETE
I've looked for similar questions on here and on the phpMyAdmin wiki but I can't find anything about it.
I realise I could do this via the query interface, but I'd like to know how to do it through the graphical interface.
First, you should have your storage engine as InnoDB. Then select a table and go to 'Structure' tab.
Under the table you will see 'Relation view', click it. From there you could add constraints.
CASCADE
Whenever rows in the master (referenced) table are deleted (resp. updated), the respective rows of the child (referencing) table with a matching foreign key column will get deleted (resp. updated) as well. This is called a cascade delete (resp. update[2]).
RESTRICT
A value cannot be updated or deleted when a row exists in a foreign key table that references the value in the referenced table. Similarly, a row cannot be deleted as long as there is a reference to it from a foreign key table.
NO ACTION
NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.
SET NULL
The foreign key values in the referencing row are set to NULL when the referenced row is updated or deleted. This is only possible if the respective columns in the referencing table are nullable. Due to the semantics of NULL, a referencing row with NULLs in the foreign key columns does not require a referenced row.
Firstly, you should choose storage engine as InnoDB.
Follow this way: click database_name -> More -> Designer

Export and import foreign Keys

We've tranfered a site to a new server and now it's been running for three days without any foreign keys being active.
How can I:
Export the foreign keys from the old structure (100+ tables)
Import the keys while ignoring integrity problems
Discard all the records where the foreign key are missing
Create two dumps (current/imported) and use a database diff tool, or generate the alter table statements by pulling the data from the information schema.
SET foreign_key_checks = 0; to disable, SET foreign_key_checks = 1; to enable.
Use left join statements and search for null values in the outer table.