Create MySQL trigger before update any table - mysql

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.

Related

Mysql database change event

I need to call bash script when mysql database schema has changed. As example queries:
ALTER TABLE, CREATE TABLE, DROP TABLE
it possible?
Don't think that's possible any way. You could have probably use a DDL Trigger but MySQL doesn't support one. See worklog https://dev.mysql.com/worklog/task/?id=2418.
Though you can write a stored procedure to perform the business logic and call that procedure but capturing the DDL event isn't possible AFAIK. You should also check on Event Scheduler in MySQL

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

When should i create mysql trigger?

Are triggers specifics to each mysql connection? Or is it done when i create trigger one time?
A trigger is created on a table, not on a connection, and once defined it remains on the table until it is explicitly dropped. It can be defined to fire for INSERT, UPDATE and/or DELETE operations on the given table.

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.