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.
Related
I'm using symfony 4 and when I do links between tables there are indexes created. I do not understand how they work.
What is their purpose and why is it impossible to import data file into those files.
Is it possible to 'bypass' this mysql restriction and import data-files into mysql even if there is a constraint (without breaking the indexes)?
Thanks
MySQL is a relational database system. If there is a relationship between 2 tables, it is there for a reason. Think of your entities in Symfony. If one entity (a primary entity) has several related entities and those related entities can only exist if there is a primary entity, then the constraint must exist to prevent orphaned records.
For example:
Consider the relationship between companies and divisions. A company can have 0 or more divisions, but a division can only exist as a part of a company. In this case, a record in the division table would have a reference to a record in the company table. However, a record in a company table would have no direct reference in MySQL to any records in the division table.
To prevent a division from being created without a company, you cannot insert a row into the division table that does not reference a record in the company table.
To finally answer the question directly, you can only import data that does not reference the primary table by removing the foreign key constraint (this will not destroy the index). Keep in mind that this will likely result in records that are "orphaned" and do not fit the business model you are trying to create.
Rather than removing the foreign key constraint, you should first import the data to the referenced table (scoring?) and then update the data being imported to contain the correct ids to reference the primary table.
Based on the information in the image provided, each record in the file being imported should have a value for scoring_id that is equal to (presumably) the id field in the scoring table. If there is some other piece of data that can be used to link the 2 tables, use that and configure your entities appropriately.
Mysql FOREIGN KEY is used for data integrity. So what happens is you have a Foreign key which refers to column in another table .It's a way to link data relationships between tables
When Using Import if that key does not exist in the other table the mysql import will throw errors .
If you remove the FOREIGN KEY flag you would be able to import that table
refer to MYSQL manual
on Foreign Keys
Foreign keys will check if the other table has already that element that you try to enter.
What you can do for the import temporarily
use FOREIGN_KEY_CHECKS
Before the import run
SET FOREIGN_KEY_CHECKS=0;
and When it is finished
SET FOREIGN_KEY_CHECKS=1;
Or
use DISABLE KEYS:
ALTER TABLE table_name DISABLE KEYS;
and when the import is finished:
ALTER TABLE table_name ENABLE KEYS;
Of course you have to change table_name
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;
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.
Is it possible to disable the Mysql foreign key check for specific tables?
Basically there is not built-in functionality for that in MySQL.
You are able to disable FKs - as you may know - but not for a specific tables - but all.
Below is an example how it works only for a session (from MySQL 5.xx you can do it globally).
SET FOREIGN_KEY_CHECKS = 0; #Off
SET FOREIGN_KEY_CHECKS = 1; #On
But you could handle this in a different way - with stored procedures.
In theory you could build stored procedure that drop foreign keys from particular tables, save all this (ie table, FK name, references) to a table so you could retrieve all that later and based on that re-create the same FKs you had before.
But the question here is also about timing - for how long you would like to make them disabled- and primary what is reason behind (some big data inserts, updates, deletions etc).
I m trying to import data from one table to another table across the schema.but foreign key constraint is giving issue.
Suppose i m having schema one and schema two.
schema one has tables -->
user
behavior
userbehavior(id from user and behavior table are foreign keys in
userbehavior table)
same way i have tables structure in schema two.
schema 2 has tables-->
user1
behavior1
userbehavior1(id from user and behavior table are foreign keys in userbehavior table)
I have successfully imported records from user to user1 and behavior to behavior1 but when I m trying to import data from userbehavior to userbehavior1 i m getting following error::
cannot add or update a child row.foreign key constraint fails.
wat could be an issue?
Thanks in advance.
You have inconsistent data, which the database refuses to import, and for a good reason.
I'd create TEMPORARY tables (i.e. tables that are automatically deleted after the session ends, import the data there, use a few queries that show me the rows from userbehaviour that violate the FOREIGN KEY constraint, clear these up manually and then use SELECT INTO to copy the data to the real tables.
cause : There is at least one row in table userbehaviour which have no parent in other table
todo: first remove all foreign key constraints from both schema and Insert and then set constraint back
In case of exporting
open exported sql file and write
SET FOREIGN_KEY_CHECKS=0; on the top of the file
and SET FOREIGN_KEY_CHECKS==1; on the bottom of file
or
export through phpMyAdmin then check Disable foreign key checks