SQL - Modify or Delete a Trigger - mysql

I created triggers, and I can see them when I do SHOW TRIGGERS. How is it possible to either modify or delete them?
I use MySQL

You cannot alter a trigger, while you have to drop it and then create the new one.
The drop syntax is
DROP TRIGGER trigger_name

create or replace trigger <trigger_name>
using above statement you can modify the trigger .

delete trigger:
drop trigger trigger_name
modify trigger:
drop trigger trigger_name
create trigger trigger_name ...
There is currently no alter trigger at the moment. SO you have to drop and recreate a trigger if you want to modify it.
TRIGGER SYNTAX

Related

Can I write a trigger in MySQL to take effect both in AFTER DELETE and AFTER UPDATE?

Just like this:
CREATE TRIGGER `adu_order` AFTER DELETE UPDATE ON `order`
But it's not ok. And I got an error prompt when trying to create it.
Then is it possible to achieve such a goal?
You can not specify two events at once. " AFTER DELETE UPDATE" is not supported. You have to pass one. See the manual for trigger. Hope it works for you.
CREATE TRIGGER trigger_name
[BEFORE|AFTER] [INSERT|UPDATE|DELETE] ON table_name
FOR EACH ROW [FOLLOWS|PRECEDES] existing_trigger_name
BEGIN
…
END

MySQL 5.0 can not drop and create trigger

I'm using MySQL 5.0. I try to drop and recreate the trigger.
When I drop the trigger it says:
mysql> drop trigger ads_delete;
ERROR 1360 (HY000): Trigger does not exist
Then I try to create the trigger with the same name. It says:
ERROR 1359 (HY000): Trigger already exists
Here is my trigger:
delimiter //
create TRIGGER ads_delete
BEFORE INSERT ON ads
FOR EACH ROW
BEGIN
update params set ads_count=ads_count-1, freq_weight=freq_weight-NEW.freq;
END//
You need to drop the trigger like this:
USE db5;
DROP TRIGGER ads_delete;
Your trigger is in the db5 schema.
EDIT:
As OP commented that the problem is
BEFORE INSERT ON ads
ie, he is trying to create two triggers with the same instruction. (Probably a copy paste issue)
I found the error. The reason was my inadvertence. I copy/pasted my INSERT trigger, when changed the trigger name from ads_insert into ads_delete, I forgot to change BEFORE INSERT ON ads into BEFORE DELETE ON ads
Thanks to everyone.

What is the right way to modify a MySQL trigger?

As you probably know, there's no syntax that modifies a MySQL trigger.
To do that, you need to execute DROP TRIGGER and then re-create it again with the new definition.
What is the right/best way of doing this, considering the following:
You cannot encapsulate these two statements in a transaction as that will be pointless (both DROP TRIGGER and CREATE TRIGGER invoke implicit transactions)
You cannot use LOCK TABLES READ as an error is triggered
Just between your DROP and CREATE TRIGGER, some other session might insert/update/delete row(s) which won't be handled by neither of your new nor old triggers.
When testing before I posted, I overlooked what type of LOCK I'm acquiring, it was READ.
So it seems using WRITE lock does the job:
delimiter $$
LOCK TABLES table1 WRITE $$
DROP TRIGGER IF EXISTS after_insert_on_table1 $$
CREATE TRIGGER after_insert_on_table1 AFTER INSERT ON table1
FOR EACH ROW
BEGIN
...
END
$$
UNLOCK TABLES $$
delimiter ;
So my recommendation is to always use this sequence when updating/modifying triggers.

MYSQL - Simple after insert trigger won't fire

Here I have the following trigger:
USE dbsspf;
DELIMITER $$
CREATE
DEFINER = 'root'#'localhost'
TRIGGER TR_ASSIGN_PAGEINDEX
AFTER INSERT
ON LIB_RECORDS
FOR EACH ROW
BEGIN
UPDATE LIB_RECORDS
SET
PAGE_INDEX = 13;
END
$$
DELIMITER ;
As you can see I'm just updating the table in my trigger. However when I insert a new record the trigger does not fire. Can you please show me what I've missed?
B.5.9: Can triggers access tables?
A trigger can access both old and new data in its own table. A trigger
can also affect other tables, but it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
http://dev.mysql.com/doc/refman/5.5/en/faqs-triggers.html#qandaitem-B-5-1-9
In other words, a trigger on LIB_RECORDS cannot write other rows on LIB_RECORDS.

MySQL trigger to fire on alter or drop

I've encountered an interesting problem. I would like to create two triggers, trigger1 would fire on DROP TABLE and trigger2 on ALTER TABLE. I would like trigger1 and trigger2 to fire if any table is dropped or altered, but, unfortunately I don't know the syntax to do that and I couldn't find a syntax for such triggers either. In MySQL I can only write triggers which fire before/after INSERT, UPDATE, or DELETE, but now I would like to write triggers which would be applicable on database level and would fire on table events. Can somebody help me?
What you are looking for is known as "DDL Triggers". MySQL does not support them
There is this SourceForge request but I'm not sure how serious anyone is about adding it
It's not possible to use a trigger for drop statements.According to the documentation the trigger is activated on
INSERT: The trigger is activated whenever a new row is inserted into the table; for example, through INSERT, LOAD DATA, and REPLACE
statements.
UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE statements.
DELETE: The trigger is activated whenever a row is deleted from the table; for example, through DELETE and REPLACE statements.
However, DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE.
It doesn't say anything about alter table, but I would expect only DML-statements to be able to fire a trigger.
PROCEDURE `pr_new_type`( IN column_name varchar(10) )
BEGIN
SET #queryText = CONCAT('ALTER TABLE `user_rights` ADD ', column_name, ' BINARY( 9 ) NULL');
PREPARE query FROM #queryText;
EXECUTE query;
DEALLOCATE PREPARE query;
END
test this