MySQL foreign key constraints, cascade delete

前端 未结 3 498
一个人的身影
一个人的身影 2020-11-22 06:19

I want to use foreign keys to keep the integrity and avoid orphans (I already use innoDB).

How do I make a SQL statment that DELETE ON CASCADE?

If I delete a

3条回答
  •  抹茶落季
    2020-11-22 07:07

    I think (I'm not certain) that foreign key constraints won't do precisely what you want given your table design. Perhaps the best thing to do is to define a stored procedure that will delete a category the way you want, and then call that procedure whenever you want to delete a category.

    CREATE PROCEDURE `DeleteCategory` (IN category_ID INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    MODIFIES SQL DATA
    SQL SECURITY DEFINER
    BEGIN
    
    DELETE FROM
        `products`
    WHERE
        `id` IN (
            SELECT `products_id`
            FROM `categories_products`
            WHERE `categories_id` = category_ID
        )
    ;
    
    DELETE FROM `categories`
    WHERE `id` = category_ID;
    
    END
    

    You also need to add the following foreign key constraints to the linking table:

    ALTER TABLE `categories_products` ADD
        CONSTRAINT `Constr_categoriesproducts_categories_fk`
        FOREIGN KEY `categories_fk` (`categories_id`) REFERENCES `categories` (`id`)
        ON DELETE CASCADE ON UPDATE CASCADE,
        CONSTRAINT `Constr_categoriesproducts_products_fk`
        FOREIGN KEY `products_fk` (`products_id`) REFERENCES `products` (`id`)
        ON DELETE CASCADE ON UPDATE CASCADE
    

    The CONSTRAINT clause can, of course, also appear in the CREATE TABLE statement.

    Having created these schema objects, you can delete a category and get the behaviour you want by issuing CALL DeleteCategory(category_ID) (where category_ID is the category to be deleted), and it will behave how you want. But don't issue a normal DELETE FROM query, unless you want more standard behaviour (i.e. delete from the linking table only, and leave the products table alone).

提交回复
热议问题