Delete multiple rows while using the id from the row before

Thomas Bang Thomas Gaming

I'm trying to create a folder and file system in my database.

And while deleting the folders with the subfolder, it should also delete the corresponding files in the folder from my other table (files_plus). But how can i delete both the folder, it's subfolder and all files contained in the folder and subfolders?

Table: files_plus:

-----------------------------
| id  |  name  |  parentid  |
-----------------------------

(the "parentid" being the "id" from the folders_plus table.)

Table: folders_plus

SQL Table example

I can't think of any way to construct a query for doing so, so how would i manage to do this?

Ivar

You can add a foreign key constraint and set it on "CASCADE DELETE" like this for the folders_plus table.

ALTER TABLE `folders_plus` 
ADD INDEX `folders_plus_parentid_idx` (`parentid` ASC);
ALTER TABLE `folders_plus` 
ADD CONSTRAINT `fk_folders_plus_id_folders_plus_parentid`
  FOREIGN KEY (`parentid`)
  REFERENCES `folders_plus` (`id`)
  ON DELETE CASCADE
  ON UPDATE NO ACTION;

And for the files_plus table:

ALTER TABLE `files_plus` 
ADD INDEX `fk_files_plus_id_folders_plus_parentid_idx` (`parentid` ASC);
ALTER TABLE `files_plus` 
ADD CONSTRAINT `fk_files_plus_id_folders_plus_parentid`
  FOREIGN KEY (`parentid`)
  REFERENCES `folders_plus` (`id`)
  ON DELETE CASCADE
  ON UPDATE NO ACTION;

When you delete a parent row, it will also delete all of it's children.

Do make sure that your parentid column is nullable, and that every top level directory is null. Every parentid that is not null, is required to exist as an id in your folders_plus table.

PS: Please do note that in order for foreign key constraints to work, your database storage engine should be InnoDB.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete multiple rows by file name using Coldfusion

From Dev

delete multiple rows in gridview using checkboxes

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Delete multiple rows using IDs?

From Dev

UPDATE & DELETE multiple rows with an array for the id

From Dev

Merge multiple rows with same ID into one row

From Dev

SQLite - How to delete parent row while keeping the child/children rows?

From Dev

Delete rows from multiple tables as one query

From Dev

delete row id using Contentprovider

From Dev

How to delete multiple rows from multiple tables using Where clause?

From Dev

SQLite select query to get one row from multiple rows have same id

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

Trying to delete selected row from datagridview but it is deleting multiple rows

From Dev

Delete multiple Items from a List of Id's using Entity Framework

From Dev

Stored procedure: delete rows from multiple tables with output for every row

From Dev

Delete multiple rows by file name using Coldfusion

From Dev

How to combine values from multiple rows into a single row using module?

From Dev

Delete row + the following 2 rows if a certain row name using UNIX

From Dev

Get row with the lowest id from multiple rows?

From Dev

How can i delete from the database multiple rows using a checkbox?

From Dev

Delete a row javascript using row id

From Dev

Delete multiple rows using IDs?

From Dev

How do I delete a row when multiple rows have the same id

From Dev

UPDATE & DELETE multiple rows with an array for the id

From Dev

Delete row in excel while exporting using php

From Dev

How do I delete a row from Sqlite using ID where my ID is binary data (GUID)

From Dev

select single row from multiple rows by id

From Dev

Delete multiple rows using Jquery

From Dev

How to delete rows in multiple tables using pdo

Related Related

  1. 1

    Delete multiple rows by file name using Coldfusion

  2. 2

    delete multiple rows in gridview using checkboxes

  3. 3

    Delete multiple rows from multiple tables in mysql using single value

  4. 4

    Delete multiple rows using IDs?

  5. 5

    UPDATE & DELETE multiple rows with an array for the id

  6. 6

    Merge multiple rows with same ID into one row

  7. 7

    SQLite - How to delete parent row while keeping the child/children rows?

  8. 8

    Delete rows from multiple tables as one query

  9. 9

    delete row id using Contentprovider

  10. 10

    How to delete multiple rows from multiple tables using Where clause?

  11. 11

    SQLite select query to get one row from multiple rows have same id

  12. 12

    Postgresql delete multiple rows from multiple tables

  13. 13

    Trying to delete selected row from datagridview but it is deleting multiple rows

  14. 14

    Delete multiple Items from a List of Id's using Entity Framework

  15. 15

    Stored procedure: delete rows from multiple tables with output for every row

  16. 16

    Delete multiple rows by file name using Coldfusion

  17. 17

    How to combine values from multiple rows into a single row using module?

  18. 18

    Delete row + the following 2 rows if a certain row name using UNIX

  19. 19

    Get row with the lowest id from multiple rows?

  20. 20

    How can i delete from the database multiple rows using a checkbox?

  21. 21

    Delete a row javascript using row id

  22. 22

    Delete multiple rows using IDs?

  23. 23

    How do I delete a row when multiple rows have the same id

  24. 24

    UPDATE & DELETE multiple rows with an array for the id

  25. 25

    Delete row in excel while exporting using php

  26. 26

    How do I delete a row from Sqlite using ID where my ID is binary data (GUID)

  27. 27

    select single row from multiple rows by id

  28. 28

    Delete multiple rows using Jquery

  29. 29

    How to delete rows in multiple tables using pdo

HotTag

Archive