Thursday, August 27, 2009

Create MySQL Triggers

I need to create a trigger in MySQL in the GUI tool Query Browser. However, I kept running into syntax errors. It turns out that I had to specify a delimiter other than ";" like the following.

Delimiter $$

CREATE TRIGGER tr_skill_path BEFORE INSERT ON skill
FOR EACH ROW
BEGIN
DECLARE parent_path CHAR;

IF NEW.parent_skill_id is not null THEN
select path into parent_path from skill where skill_id = NEW.parent_skill_id;
END IF;

IF parent_path is null THEN
SET NEW.path = CAST(NEW.skill_id AS CHAR);
ELSE
SET NEW.path = CONCAT(parent_path, '.', CAST(NEW.skill_id AS CHAR));
END IF;
END$$

Delimiter ;

This made it pass the syntax error. But another error came up:
Access denied; you need the SUPER privilege for this operation

According to MySQL Manual, "SUPER privileges are administrative and can only be granted globally". So it won't work if the grant is only for the schema. More on this later...

No comments:

Post a Comment