For this error message, is there a way for mysqlimport to output the specific row that fails the constraint? It's good for debugging. Otherwise, I don't know which row has this problem.
mysqlimport: Error: 1452, Cannot add or update a child row: a foreign key constraint fails (`bwordDS`.`Category_Term`, CONSTRAINT `FK_qn1crq26sr0hqm5q2p7nfvdu1` FOREIGN KEY (`categories_id`) REFERENCES `Category` (`id`)), when using table: Category_Term
First check if Category table exist where you are trying to import this data.
if Category table exists then need to check that all category_id in this table should exist in Category table as id.
Better option is to import first category table and then this table.
In generic first all parent tables data should import then child tables.
A dirty way is as per below which is not recommended-
set foreign_key_checks=0;
import data here;
set foreign_key_checks=1;
Just to know which row is creating problem-
Below query will provide you problematic rows.
SELECT a.category_id FROM Category_Term a
LEFT JOIN Category b ON a.category_id=b.id
WHERE b.id IS NULL;
Note: Assuming category_id in category_term and id in category tables will be indexed.
I used the sample query provided by #Zafar, but I added DISTINCT to SELECT as well:
SELECT DISTINCT countries.city_id
FROM countries
LEFT JOIN cities ON countries.city_id=city.id
WHERE city.id IS NULL;
Related
I'm trying to find all rows in table 'wpi9_pc_products' that have less than 3 entries in table 'wpi9_term_relationships', so that I can delete them from both.
Before shooting from the hip, I want to ensure I'm pulling the right rows for deletion, using this query:
SELECT * FROM `wpi9_pc_products` INNER JOIN wpi9_term_relationships ON
wpi9_pc_products.id_product=wpi9_term_relationships.object_id GROUP BY
wpi9_term_relationships.object_id HAVING COUNT(*) < 3
However, I'm stuck here trying to figure out the correct way to join the two tables conditionally based on the count.
My SQL is very, very rusty, so any help would be greatly appreciated!
If I am following correctly, you have a parent/child relationship between the products and the relationships table. I would recommend setting up a foreign key constraint on the child table, that references the parent table, like so:
alter table wpi9_term_relationships
add constraint fk_product_id
foreign key (object_id)
references wpi9_pc_products(id_product)
on delete cascade;
Then, you can simply delete the records from the parent table; the on delete cascade clause ensures that the children records will be automatically deleted:
delete p
from wpi9_pc_products p
where (select count(*) from wpi9_term_relationships r where r.object_id = p.id_product) < 3
table 1 is called (athlete) and table2 is called (training_session.id) the primary key to table 1 is ID, and the table 2 has the primary key Athelete_id
I want to delete a person from my database by using his name, which I've called "Pet". However, he is also connected to another table which stores his training session. So (ID 1) on table 1 is connected to table 2 (athlete id1)
I struggle a lot, I try using INNER JOIN.
DELETE athlete,training_session FROM athlete
INNER JOIN
training_session ON training_session.id = athlete.name
WHERE
athlete.name = "Pet;
I have something wrong with my syntax, is it correct to use Inner Join or have I misunderstood
You should have set up foreign key constraints with Cascade deletions to simplify the logic and all you would have needed than was to delete from athlete. So I would suggest you add it.
For more info you can take a look at:
http://www.mysqltutorial.org/mysql-on-delete-cascade/
I do not understand MySQL delete when I need to delete data in a table with data from another table that depend on it.
For example if I want to delete a data in table 'factory', all data at table 'room' that depends on data at table 'factory is also deleted'.
Fac_ID is the primary key in 'factory' and foreign key in 'room'
below is my SQL code.
DELETE * FROM factory
LEFT JOIN room ON room.Fac_ID = factory.Fac_ID
WHERE factory.Fac_ID = :Fac_ID
Can any one help me?
I think you need a separate delete for this.
First is to delete foreign data
delete from room where Fac_ID = :Fac_ID
Then delete primary data
delete from factory where Fac_ID = :Fac_ID
Unless your table design is ON DELETE CASCADE (supported only in INNODB), you only need to delete the primary data
There is a small mistake in your query because of which I think you are facing problem.
As per my understanding you have some records in Main table and some records in refrenced table. There are some case for which main table has some id but there is not entry in refrence table for that id. And for handling that case you applied left join.
But in your query you wrote reference table on left so basically it is taking all of the record from reference table which is kind of inner join in this case.
So for correcting this you need to interchange the key id pass in your query or you may use right join to select all records from main table.
DELETE * FROM factory
LEFT JOIN room ON factory.Fac_ID = room.Fac_ID --- here you applied left join
WHERE factory.Fac_ID = :Fac_ID
MySQL allows you to delete rows from multiple tables at the same time. The syntax is:
DELETE f, r
FROM factory f LEFT JOIN
room r
ON r.Fac_ID = f.Fac_ID
WHERE f.Fac_ID = :Fac_ID;
However, this is better set up as a cascading delete foreign key relationship between the two tables.
I have read everything I could find on the web regarding JOIN and foreign key relationships, but can not seem to get my head around a solution to solve my problem.
As you can see in the relationship diagram image my CUSTOMER table have a foreign key relation with TASK_IN table.
TASK_IN table have a foreign key relation with TASK_ID table.
I want to retrieve all data from the TASK_ID table. Can this be done via the CUSTOMER and TASK_IN table foreign key relation - if yes, how would the SQL query look (PHP)?
SELECT * FROM task_id AS taskid INNER JOIN task_in AS taskin ON taskid.id = taskin.id INNER JOIN customer AS cust ON taskin.id = cust.id WHERE id = YOUR_VALUE_HERE
EDIT:
this is just basic sql syntax, you can read more here.
I ve got a database modelize like this,
One mother table let's call it table_mother, and severals child tables.
Relation beetween table_mother and childs is like this:
All tables childs have a foreign key kind of name as the id of the mother table (id_table_mother) (relationship is 1->n as id_table_mother is uniq and tbale child can get several entries for id_table_mother)
I would like to delete all records in childs table wich are related no more with the mother table, for now i try something like this
DELETE FROM tb_child_1,tb_child_2,tb_child_3
WHERE
tb_child_1.id_table_mother
AND tb_child_2.id_table_mother
AND tb_child_3.id_table_mother
NOT IN (SELECT id_table_mother FROM tb_table_mother);
Thx
edit : this is how I ended for now
delete from tb_child_1 where id_mother not in (select id_mother from tb_mother_table);
delete from tb_child_2 where id_mother not in (select id_mother from tb_mother_table);
any "global" solution ?
also my database is not innodb so I can 't go with foreigh keys and stuff
You have to build FOREIGN KEY Constraints to be performed on delete or update to know more about FOREIGN KEY Constraints visit http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Write 3 queries like this one -
DELETE
tb_child_1
FROM
tb_table_mother
LEFT JOIN
tb_child_1
ON tb_table_mother.id_table_mother = tb_child_1.id_table_mother
WHERE
tb_child_1.id_table_mother IS NULL;