MySQL Forums
Forum List  »  Triggers

Why does my Trigger not work for CASCADE-Actions?
Posted by: Martin Fernau
Date: January 25, 2006 10:09AM

Hello!

As an example: I have three tables (all InnoDB). Table a,b and c
table `b` has a foreign-Key referenced to table `a`. For this reference I declared the CASCADE-Action on DELETE. See here:

CREATE TABLE `a` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `b` (
`id` int(11) NOT NULL auto_increment,
`id_fk` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_fk` (`id_fk`)
) ENGINE=InnoDB;

ALTER TABLE `b`
ADD CONSTRAINT `b_idfk_1` FOREIGN KEY (`id_fk`) REFERENCES `a` (`id`) ON DELETE CASCADE;

Table `c` is nothing more than a simple log-Table like:
CREATE TABLE `c` (
`id_fk` int(11) NOT NULL,
`log` varchar(255) NOT NULL
) ENGINE=InnoDB;

What I now try to have is a trigger, which logs all deleted rows of table `b` like this:

delimiter |
CREATE TRIGGER b_del BEFORE DELETE ON b
FOR EACH ROW BEGIN
insert into c (id_fk, log) VALUES (OLD.id, OLD.name);
END;
|
delimiter ;

... which works, if I manually delete a row of `b`. But if I delte a row of `a` which automaticly delete a row of `b`, the trigger won't executed. Why?

Regards,
Martin

Options: ReplyQuote


Subject
Views
Written By
Posted
Why does my Trigger not work for CASCADE-Actions?
3359
January 25, 2006 10:09AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.