how to create trigger on any change in table - mysql

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

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

Create MySQL trigger before update any table

I would like to create a trigger that intercepts any update on any table in the database, is there a way to NOT specify the table for which the trigger is being made so it can intercept all updates?
Something like:
create trigger interceptor
after update on ANY_TABLE
....
This is not possible, you have to create individual triggers per table.
However, as the comment above tells, every trigger can be calling the same stored procedure: Need an abstract trigger in MySQL 5.1 to update an audit log
You may go even further by creating a procedure that loops through all tables in your DB using information_schema, dropping and then (re)creating triggers automatically, then call this script every time you update your DB structure.

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

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.

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;

how to create database trigger on insert, update and delete operation in mysql with coldfusion?

how to create trigger in mysql in coldfusion at the time of insert, update and delete operation.
Thanks
Yugal
I don't really get the point of this because the whole idea of using triggers is to automatically have the database do something without the need to do it from your server-side language on every insert/update or delete.
So basically it's code you only execute once to create the trigger.
I suppose it's just creating a trigger between your <CFQUERY> tags similar to how you do an insert, update or delete operation
Before the insert
CREATE TRIGGER triggerName BEFORE INSERT ON tableName FOR EACH ROW what_ever_you_want_your_trigger_todo;
After the insert
CREATE TRIGGER triggerName AFTER INSERT ON tableName FOR EACH ROW what_ever_you_want_your_trigger_todo;
http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
But really I would do this using a mysql-client, set and forget it. Depending on the use of BEFORE or AFTER it will be executed either before or after your insert/update/delete statement on that table.