Insert RowData in MySql Table using Trigger After Update - mysql

I am running MySQL 5.6. I want to know which row/rows have been updated in a table. One way is to create a trigger using PHPMyAdmin. This trigger can store row id (id is be a table column) of that row and current datetime into another table. This way I will know which record has changed. In PhpMyAdmin when I add trigger it asks me event, time and table which I filled. But what do I write in Definition & Definer.

Related

Copy row to column in Mysql Trigger

I'm trying to create a trigger on my table. I need save old fields to another table's JSON column.
Can I do it ?

MySQL INSERT AFTER Trigger to Copy Row to Another Database

I'm running MySQL 5.6 and I have two databases. When a record is inserted into the table of one database, I want to insert that row into the same table in the other database. I have this working properly.
My question, is there a shorthand reference to just insert the whole new row into the other database's table. Short-hand so I don't have to write all the column names and VALUES(). I'd like to simplify the trigger so that when I update the table (add/remove/rename columns), I don't need to update the Trigger's SQL code too.
Something like this (this won't work, but gets the point across), that copies the new row from database "abc" to database "def"
CREATE TRIGGER abc.users_insert_trigger
AFTER INSERT
ON abc.users FOR EACH ROW
INSERT INTO def.users NEW;

MySQL: How to pass parameter to a trigger

I have a table on a mysql 5.7 db, containing say athletes with their mean, max, avg times in a specific sport. I have another table that lists some calculated statistics based on those values.
I managed to do the calculcations that end up on the second using stored procedures. I use as input parameter to the stored procedure the athlete's name.
So when in the first table, an athlete is inserted (with his/her avg/min/max times) or his/her values are updated and I run the stored procedure, the later updates the statistics table.
My question is how to achieve the same result with triggers?
I guess it is feasible/easy to update the entire table on each insert or update of the first table. What would be more efficient performance-wise, would be on each :
INSERT into table1 values (..) where athlete_name="John Do"
(...)
ON DUPLICATE KEY UPDATE (...)
Run a trigger in the pseudocode form :
INSERT into statistics_table values (..) where athlete_name="John Do"
ON DUPLICATE KEY UPDATE (...)
How can the the athlete_name="John Do" be passed to the trigger dynamically, to avoid update the entire statistics table?
You cannot pass any parameters to a trigger and the insert statement does not support the where clause either.
Having said this, a trigger can pick up the user's name from the record being inserted / updated / deleted using NEW.athlete_name or OLD.athlete_name (whichever is required) and use that to call a stored procedure:
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case-sensitive.
In an INSERT trigger, only NEW.col_name can be used; there is no old
row. In a DELETE trigger, only OLD.col_name can be used; there is no
new row. In an UPDATE trigger, you can use OLD.col_name to refer to
the columns of a row before it is updated and NEW.col_name to refer to
the columns of the row after it is updated.
A column named with OLD is read only. You can refer to it (if you have
the SELECT privilege), but not modify it. You can refer to a column
named with NEW if you have the SELECT privilege for it. In a BEFORE
trigger, you can also change its value with SET NEW.col_name = value
if you have the UPDATE privilege for it. This means you can use a
trigger to modify the values to be inserted into a new row or used to
update a row. (Such a SET statement has no effect in an AFTER trigger
because the row change will have already occurred.)
You can create triggers that fire after each insert or update on the parent table (athletes). Within each trigger, you can access the value of column athlete_name on the record that was just created or changed, and then invoke your stored procedure using CALL().
Here is a code sample for such an INSERT trigger :
CREATE TRIGGER athletes_upd AFTER INSERT ON athletes
FOR EACH ROW
BEGIN
CALL my_procedure(NEW.athlete_name);
END;
UPDATE trigger :
CREATE TRIGGER athletes_upd AFTER UPDATE ON athletes
FOR EACH ROW
BEGIN
CALL my_procedure(NEW.athlete_name); -- or maybe OLD.athlete_name ?
END;

update column of mysql database table after record is updated

I have a strange problem. I have mysql database table. The few tables are updated from php script (I can't access the script), Now I need to update another column (count column whicih will increment by one each time the row is updated) using update trigger. Can some one help.
Here what I was looking for.
DROP TRIGGER IF EXISTS `trigger_name`;
CREATE DEFINER=`username`#`server`
TRIGGER `trigger_name` BEFORE UPDATE ON `table_name`
FOR EACH ROW
Set new.penalty = new.penalty+1
This worked for me.

Mysql triggers : OLD and NEW as a string/xml/parameter?

I would like to store all changes to my tables (in MySQL). I created a table 'audit' to track all changes of all tables in one table (so I don't have to create audit-tables for each table).
I then created triggers for each table and a stored procedure that inserts a record into the audit-table. The parameters for the stored procedure are the tablename and the primary id. Now I'm able to track the insert/update/delete dates for each record in my database.
But I also would like to trace all changes to the DATA with this procedure. For this I'd have to find a way to use the OLD and NEW records from the triggers in the stored procedure.
Anybody know how to do this?
Maybe some kind of serializing the records OLD and NEW into a string??
The only solution that I could derive was checking the OLD & NEW values of every field in my table.
BEGIN
-- record changes to table log_new
IF OLD.fieldA != NEW.fieldA
OR OLD.fieldB != NEW.fieldB
THEN
INSERT INTO log_new (