I can't make this work. I want to create a trigger that deletes the child foreign keys before I delete the parent primary key. This is the code that I am using on phpmyadmin:
delimiter //
CREATE TRIGGER removeforeign
before delete
on products
FOR EACH ROW
BEGIN
delete from sales_details
where product_code=21;
END//
delimiter ;
Right now, I can't make trigger works at all. Not for insert, delete, update. In this code, it will delete where product_code is 21 because it's only an attempt to get the syntax right, I am still to figure it out how to compare with what is being deleted. Please, help me with the syntax. I can't see why it isn't working.
Best Answer
It may be more efficient to simply use a cascading delete via a foreign key. When a row with the same
product_code
is deleted from products, it will be deleted insales_detail
as well.But if you'd like to use a trigger to delete rows from the
products
table that have the sameproduct_code
as the row deleted, this should do the trick:Edit
Per documentation,