Mysql database change event - mysql

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

Related

Working with Events in MySQL

I have a stored procedure which basically selects data from one table and insert into another table. Basically I am doing data archiving manually. Now, I want to write an event just like discussed here
However, after reading that post and researching online, I came to know that it's not possible to create an event inside a stored procedure. Is there a way to accomplish my goal in MySQLWorkbench?
I believe you are thinking this in the oposite direction: You can't create an event in a stored procedure, but you can create a stored procedure and call it from an event.
Example:
delimiter $$
create procedure myProc()
-- Dummy procedure to move the data from mainTable to backupTable,
-- and then clear (truncate) mainTable
begin
insert into backupTable select * from mainTable;
truncate mainTable;
end $$
delimiter ;
-- Now, suposing that you want to execute this procedure every hour:
delimiter $$
create event myEvent
on schedule every 1 hour
do
begin
call myProc();
end $$
delimiter ;
You can write this as any other query in the workbench, or directly in the command line client.
About your concern
After reading your comment, I believe you are a bit confused about what MySQL Workbench is.
MySQL Workbench is only a graphical application that allows you to connect to a MySQL server and perform queries and administration tasks. But Workbench is not the core of MySQL... it is only a viewer (with steroids, maybe, but a viewer after all).
Now, the event scheduler does not reside in Workbench, but in the MySQL server instance you are connecting to. Just as the tables, views, procedures and functions are not stored in the Workbench interface but in the server, the events are also stored in the server.
(Yes, I believe it is a relevant SNAFU that the scheduled events don't show anywhere in the graphical interface, but... after a while, one learns to live with that kind of frustrations and to move on with life)
Maybe your only concern is: "Hey, and what if I want to know what events are set to run in the event scheduler?" You can execute a "show events" query to show a list of the events in the current database, and you can execute "show create event yourEvent" to show the create event syntax for that event.
I insist: Read the manual, and keep a copy at hand (download the manual for your MySQL version here).

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.

MySQL Create Trigger Keywords

I know that in triggers you have the keywords NEW and OLD to refer to the entry that is being, or was, inserted into the table to which the trigger is bound. Are there any other keywords? I'm looking for one specifically that reference's the table that the trigger is bound to (like CUR_TABLE, or something); this way I can copy the trigger and apply it to several tables with different names and not need to alter the body of the trigger? Thanks in advance for any help!
Dynamic SQL cannot be used in triggers. For the trigger to exist, then the developer already knows what table he's in - so the table name should theoretically be hard coded.
If you are generating triggers, from say a stored procedure, you can generate them with variable table names - but cannot execute them (so you would have to take the result of the stored procedure and execute it separately).
See: http://dev.mysql.com/doc/refman/5.6/en/stored-program-restrictions.html
SQL prepared statements (PREPARE, EXECUTE, DEALLOCATE PREPARE) can be used in stored procedures, but not stored functions or triggers. Thus, stored functions and triggers cannot use dynamic SQL (where you construct statements as strings and then execute them).
This is not possible in MySQL. You may do a small PHP script that generates the code for each "table name" in an array :)

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.