Trigger after create table - mysql

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

Related

Is it possible to create a mysql trigger that inserts the rows of a table to another table at a specific time of day?

I am quite new to MySQL and I was wondering if I can make a trigger that inserts all the rows of a table to another table at a specific time. I tried to look at other solutions but it doesn't solve my dilemma. Is there any way that I can automatically add rows to another table on a specific time without generating a mysql query? Answers are appreciated.
Perhaps the Event Scheduler can be of use.

MySQL update trigger - find changed columns?

I have a table with 120 columns. I need to set up audit trail which would log any column if it was changed. As it is now, I guess I have to set up a trigger with condition something like this for every column:
IF(NEW.columnName != OLD.columnName)
THEN //log the old value
This would need to be done 120 times... While I would have accepted this approach 20 years ago, today I refuse to believe it's impossible to automate such a simple procedure finding changed columns automatically.
This is what I discovered so far:
Neither NEW nor OLD is a table, it's a sort of a language construct, therefor you can't do "SELECT NOW.*" or something similar.
Dynamic SQL is not allowed in triggers (this could have solved the problem).
Procedures using dynamic SQL are not allowed in triggers (seriously, Oracle, it looks like you have worked really hard to disable this feature no matter what).
I was thinking to use BEFORE and AFTER triggers in conjunction with temporary tables and variables which would have possibly solved the problem, however yet again dynamic SQL would be required. I feel like I hit a dead end.
Is there a solution to this at all?
A side question: would this be possible in PostgreSQL?
UPDATE: I found 2 potential solutions however neither of them look clear enough to me:
using EVENTS as a workaround to use triggers in conjunction with dynamic SQL workaround. I have to admit, I don't quite get this, does this mean that EVENT fires every second no matter what?
This article says that it is possible to use dynamic SQL inside trigger as long as temporary table is used with it. That is still using dynamic SQL, so I don't quite understand.
interesting, I was facing the same problem couple of years ago with implementing dynamic trigger-based audit log. The solution I came up with was to simply generate the SQL trigger code which then can be (automatically) applied to replace old trigger definitions. If memory serves, I created few SQL templates which were processed by a PHP script which in turn was outputting complete trigger definitions based on "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE ..." Yes, the trigger code was huge, but it worked! Hope that helps a little =)
i did this for one of the projects by creating a shadow table. if you are not dealing with millions of updates, this might work
when the user logs in, SET #user_id = { logged in user id }
create a trigger on the table before update to copy the row to be modified to a shadow table with the same structure ( note that you cannot have a primary key in the shadow table nor unique keys )
add additional columns to the shadow table ( modified_by, modified_on )
create a small php script to show the diff between columns - this way you dont touvh the existing php code base
if you are dealing with lots of updates and want to keep the shadow table small, a cron can be written to parse the shadow table and identify which column changed and only store this info to another table

How To Use Triggers to insert Changes in MySQL tables

i want to create triggers and i have a requirement to do the following.
When a record in a tables is edited trigger that will automatically log this change to another table called logs and where it use inside my php script
The set up of the log table will be as follows:
date
time
value before
value after
edited table
* i am new for triggers please help me my project
Your google-fu is weak my friend: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html but you will learn eventually :)
Though if this is not a project dedicated to creating triggers, I would not advice to use them. Since this " that will automatically log this change to another table called " can surely be done in business logic. Then you can control what is done from software and by the user. Maybe in the near future you will need to perform a datafix on your table - then you would have to disable the trigger, perform the fix and enable it again. That seem like a lot of work you don't really want to do.
So if possible - avoid triggers, if not - read the mysql docs on how to create them.

Create trigger only if it doesn't exist on MySQL

I'm writing a script to create a trigger on a table in MySQL, but there's a possibility that the trigger might already exist.
Is this something I need to worry about, or will creating the trigger overwrite any trigger with the same name?
I considered using a DROP TRIGGER IF EXISTS statement before the CREATE TRIGGER.... Will that have any performance penalties or other downsides?
Using DROP TRIGGER IF EXISTS is exactly the way to do it. The only downside is if you did not know about a trigger, and it has exactly the same name (possible if following naming conventions), then you would effectively lose the definition and functionality behind it.
will creating the trigger overwrite any trigger with the same name?
No, it will fail with an error, actually.

Is it true I can't edit a MySQL trigger, I have to drop it and create a new one?

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.