Reputation: 1242
I have a question about ON DELETE CASCADE, i have made thise tables as an example.
CREATE TABLE shop_articles(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
description TEXT
);
CREATE TABLE shop_articles_images(
id INT AUTO_INCREMENT PRIMARY KEY,
article_id INT,
description VARCHAR(255),
image VARCHAR(255),
FOREIGN KEY (article_id) REFERENCES shop_articles (id)
);
In order to delete all the data affiliated with the main table, i am using this ALTER TABLE command after adding the tables.
ALTER TABLE shop_articles_images
ADD CONSTRAINT shop_articles_images_ibf5
FOREIGN KEY (article_id) REFERENCES shop_articles (id)
ON DELETE CASCADE;
It seams alltough this have been added i cant delete rows from the main table, i dont want to manually delete the other affiliated tables, but delete it when delete from the main table.
Any one have experience with this, or can see what i do wrong here? does this altertable reset when the mysql server is restarted?
Upvotes: 2
Views: 41
Reputation: 521053
I think you have some confusion about how ON DELETE CASCADE
works. If your engine is MyISAM, which does not enforce foreign keys, then no cascading deletes will happen. If your engine is InnoDB, then if you delete a record from the main table shop_articles
then any records in shop_articles_images
which are linked to the main table via a key relationship will also be deleted.
Upvotes: 2