When should i create mysql trigger? - mysql

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.

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

Mysql limit insert/update/delete to being called from trigger (Not from application code)

My use case is that I have a MYSQL table which has a change tracking table behind it. Both tables are InnoDB.
I'd like to use triggers to enforce two things.
One: forcing a copy to be made of previous state on every update (update trigger performs insert select query) This is non-trivial and I have already done it.
Two: limit access to the second table to read only for users, while triggers can still insert/update/delete as necessary
Further research on user access and triggers has dug up this:
Mysql 5.5 reference manual: 19.6 Access Control for Stored Programs and Views
Turns out that stored programs can be assigned a user to run as. Triggers are stored programs. By restricting Insert, Update, and Delete to a special user which only will be used by a trigger and using the DEFINER attribute of the trigger (or any stored program) I can make the trigger which I want to have access run under that user.

How do i fire a trigger while truncating a table?

I am using MS SQL server 2008. Right now am firing my trigger on deleting records from my Master table to delete corresponding records from my child table. Now am trying to fire the trigger on truncate on my Master table. Is it possible? If yes, kindly help me to find the solution.
TRUNCATE TABLE cannot activate a trigger because the operation does
not log individual row deletions.
Ref.
Suggest you perform an actual delete, and perform in batches if this was the original reason (i.e. locking) you used TRUNCATE instead of DELETE

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.