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.
Related
So I want to write a trigger that updates (insert, update, delete) a table if another table(in another database) gets updated, something like this for example:
CREATE TRIGGER new_data
AFTER INSERT ON account
FOR EACH ROW
INSERT INTO test4.bank3
SET
money = NEW.amount
The problem is that I only have reading access to the other database (in this example where account lies on).
Is there a way around it or do I have to use a completely different method?
Can you ask the admin of the other database to set up a connection / trigger so that a duplicate table is made in your database? Then you could use that table as the trigger?
I need when I create a new table, insert it with the name of the table in another already existing table. I had planned to use a trigger, but I can not find documentation on how to do this. Do you have any idea?
Thank you!
There's nothing in MySQL allowing the definition of a trigger to fire upon table creation, sorry to say.
A trigger is something that happens automatically based on an event in the database. Typically speaking, it’s not a good idea to be creating tables automatically like this. What was your thinking around wanting a table to be created automatically by an event? Normally triggers would be adding or changing individual rows in a DB.
Documentation for mysql triggers can be found at https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
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.
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.
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.