Can you modify an existing mysql trigger after it has been created? - mysql

In mysql I can create a trigger, and then show information about it like this:
mysql> show triggers like 'fooTrigger';
This command gives output that looks an awful lot like a select statement, with a row showing the matching trigger. Is it possible to update a column on the row it shows me?
For example, one column is named Statement, and it defines what happens when the trigger is activated. Is it possible to change the Statement field for fooTrigger so the trigger does something different? Or do I need to drop and re-create the trigger?

As of MySQL 5.5, you must drop and re-create the trigger.
It is not possible to update the Statement column without dropping the trigger.
Documentation: CREATE TRIGGER DROP TRIGGER
You may also access information about triggers using the INFORMATION_SCHEMA tables:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
But, as these tables are actually views, you can't use UPDATE on them.
It's just a more convenient way to access and manipulate the information than using SHOW TRIGGERS.
Documentation: INFORMATION_SCHEMA.TRIGGERS

You may require, on Windows OS, Connector/Net Visual Studio Integration to view and edit existing database object. Limitation is that you can only edit trigger body within For each row ... loop.
Otherwise only option one has is drop and re create the trigger.
But make sure before dropping a trigger that the table associated with a trigger is locked and
and unlocked after trigger is re-created.

As #Jocelyn mentioned you can't alter the trigger. But if you're using MySql Workbench it will allow you to alter the trigger. Just right click on your table name and click Alter table option from there you can pick Trigger option and alter it. Although you cannot perform it from query.
Table Name --> Alter Table --> Triggers.

Related

Can you create TRIGGER that is run if a TABLE is DROPPED?

Is is possible to create a TRIGGER that can detect if a TABLE is DROPPED using MySQL 5.6.10?
I have a many databases with the same table and I want to detect if and when a that table is either updated or dropped and replaced.
Thanks
The three trigger events are INSERT, UPDATE and DELETE, with options for BEFORE and AFTER.
There's no trigger for DROP.
The same is true for MySQL 5.7 and 8.0.
Reference: https://dev.mysql.com/doc/refman/5.6/en/create-trigger.html

how to create trigger on any change in table

I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END

MySQL trigger which triggers on either INSERT or UPDATE?

Is there a way to create MySQL trigger which triggers on either UPDATE or INSERT?
Something like
CREATE TRIGGER t_apps_affected BEFORE INSERT OR UPDATE ...
Obviously, the above don't work. So, any workarounds without creating two separate triggers?
I need this in order to update running counter on another table.
Unfortunately, there is no shorthand form - you must create multiple triggers - one for each event.
The doc says:
trigger_event indicates the kind of statement that activates the trigger. The trigger_event can be one of the following:
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. Dropping a partition
does not activate DELETE triggers, either. See Section 12.1.27,
“TRUNCATE TABLE Syntax”.
While it is impossible to put a trigger on multiple events, you can define the two triggers to merely call another stored procedure and, with that, cut down on the amount of code you need to commit. Just create the separate triggers to do nothing but, say,
CALL update_counter();
and put all of your actual work into the procedure. The triggers would then be a simple
CREATE TRIGGER t_apps_affected BEFORE INSERT ON table
FOR EACH ROW
BEGIN
CALL update_counter();
END;

Is it true I can't edit a MySQL trigger, I have to drop it and create a new one?

Is it true I can't edit a MySQL trigger, I have to drop it and create a new one?
Also, being a relative newcomer to triggers, it feels like they seem liable to causing 'erroneous' data. For example I might want a trigger to be fired (inserting data into another table) after one particular type of update query, but not others.
Any tips here gratefully received!
Edit: Yes, it is true that versions 5.n and 6.n of MySQL 5 & 6 implement CREATE TRIGGER and DROP TRIGGER and nothing else. According to this hunk of Postgres documentation, there is not even CREATE TRIGGER in SQL 92, so consider yourself lucky to have TRIGGER at all :-)
The Visual Studio MySQL plugin documentation has:
To modify an existing trigger, double click on a node of the trigger you wish to modify, or right click on this node and choose the Alter Trigger command from a context menu. Either of the commands opens the SQL Editor.
... which seems to do what you want. My guess is this is GUI sugar and behind the scenes you get a DROP CREATE.
As far as a trigger for some UPDATEs and not others, SQL has exactly one UPDATE per table. Put an IF clause at the start of your UPDATE trigger so that your logic - whatever you are doing in some of your UPDATEs - is only executed when you think it is appropriate.
MySQL has REPLACE TRIGGER, right?
As a sidenote.. Is it an issue? If you're worried queries are executed in between DROP and CREATE, you could always lock the table beforehand.
If you're using MySql Workbench it will allow you to alter the trigger. Just right click on your table name and click Alter table option from there you can pick Trigger option and alter it. Although, you cannot perform it from query mode.
Table Name --> Right Click --> Alter Table --> Triggers.

Using MySQL triggers to create database on insert

Can I use a trigger in MySQL to create a new database when a value is inserted into another database.
There is definitely a design problem in why you want to do this. (Create a database in trigger). As it turns out this is not allowed. This is a list of what all is allowed to be placed in a trigger.