MySQL INSERT AFTER Trigger to Copy Row to Another Database - mysql

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;

Related

How to auto update a master table in MySQL when new rows are added to sub tables?

I am working in mysql workbench on an inventory database. I have smaller tables for sub-categories of inventory (mens, womens, etc). I am attempting to use a trigger to auto-update the master inventory table whenever a new row is added to one of the other tables. My current code is:
create trigger update_master
after insert on boys_inventory
for each row
insert into master_inventory;
I am getting an error with the SQL syntax. Any suggestions?

Insert RowData in MySql Table using Trigger After Update

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.

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.

Combine three tables to make an updatable new table

Is there any way (except views) to create a new table in mysql that combines data from three or more different tables and when inserting new records into the initial tables data are also displayed in the new merged table?
Introduction
One thing though except for views, would be to use triggers.
When an update is made on this new table, your on insert or on update triggers could call a stored procedure to update the related data.
How to Pages:
See the following related pages:
mysql after insert trigger which updates another table's column
Using an update trigger to update another table
Update another table after insert using a trigger?