Mysql: delete rows in two tables with foreign keys

前端 未结 3 1016
后悔当初
后悔当初 2021-01-03 03:04

I have two tables with data. I want to delete rows in both tables. But I have foreign keys between them. How can I do this?

 departure
     id    departure_d         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 04:03

    Please try this, hope it will help.

    DELETE FROM departure, departure_time
    USING departure
    INNER JOIN departure_time
    WHERE departure_date = '2016-09-30'
          AND departure_time.id = departure.id
    

    Or

    DELETE FROM departure, departure_time
    USING departure
    INNER JOIN departure_time
    WHERE departure_date = '2016-09-30'
          AND departure_time.departure_id = departure.id
    

    Or you can use ON DELETE CASCADE that will do work automatically for you .

提交回复
热议问题