-4

I have 5 tables which are

Users

-id
-influencer_id

Influencers

-id

Categories

-catogory_id
-influencer_id

platforms

-influencer_id
-platform_id

tasks

-influencer_id
-task_id

I want to delete an influencer and also delete all records at once. How to do that?

James Z
  • 12,209
  • 10
  • 24
  • 44
FJ Shen
  • 41
  • 2
  • 5
  • If you have actual foreign keys setup on your database, you can specify that on delete have it remove foreign records. – Jonathan Kuhn Jun 05 '15 at 16:30
  • possible duplicate: http://stackoverflow.com/questions/1233451/delete-from-two-tables-in-one-query – chris Jun 05 '15 at 16:32

2 Answers2

0

use ON DELETE CASCADE while creating tables. Like:

CREATE TABLE child (
    id INT, 
    parent_id INT,
    INDEX par_ind (parent_id),
    FOREIGN KEY (parent_id) 
        REFERENCES parent(id)
        ON DELETE CASCADE
) ENGINE=INNODB;

http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Bishal Paudel
  • 1,896
  • 2
  • 21
  • 28
0

First delete records from other tables. You can use following query:

DELETE FROM Users u
USING Influencers i
WHERE u.id = i.id;

You can do the same for other tables and then at last DROP the Influencers table.

DROP TABLE Influencers;
vivekpansara
  • 895
  • 1
  • 6
  • 14