How to delete all rows of a table with a recursive structure (MySQL)?

I have a table in my database in which each row has a parent identifier, which is the identifier of another row in the table (the table represents a tree structure). I would like to clear the table. But when I do

DELETE FROM table_name WHERE true;

I get an error (foreign key constraint). How do I care about discrediting the table?

Clarification: I want to delete the entire contents of the table, not the tables themselves.

+5
source share
5 answers

When creating foreign key relationships, you need to specify on delete cascade.

EDIT: there is a pretty good link here: http://en.wikipedia.org/wiki/Foreign_key

+4

:

TRUNCATE table_name;
+1

ON DELETE, , :

DELETE FROM table_name WHERE id NOT IN (SELECT parent_id FROM table_name)
+1

, .

, id - parent_id, - :

DELETE FROM table_name WHERE parent_id IS NOT NULL;

:

DELETE FROM table_name;
0

, , - .

, , delete.

UPDATE table_name SET parent_id=null WHERE true;

DELETE FROM table_name WHERE true;
0

All Articles