Combine three tables to make an updatable new table - mysql

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?

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?

Audit Log Script using Triggers

Audit Logs for 50 tables need to insert in one table called audit table whatever the event performed on the tables(insert,update,delete) occurs it should contain new_val,old_val and table name,class name,modified by like these fields into my Audit Table.50 tables data should contain only in one single table called Audit Table
It is showing that i need to write script for every table how many tables it contains
If you will use triggers on your other tables to insert rows into your audit log table, you will indeed need to write three triggers for each table. (ON INSERT, ON UPDATE, ON DELETE).
Pain in the xxx. Triple pain in the xxx. Sigh.
If this were my project I'd consider writing a program to query information_schema.TABLES for a list of the tables involved, and generate the CREATE TRIGGER code for the tables involved. But, depending on the complexity of your table structure, that might be more trouble than it's worth.
Maybe helpful: create trigger insert update delete in 1 syntax

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;

How to create batch jobs in Mysql?

I would like to create batch jobs in a Mysql database. To fetch data from child tables, and insert them into a master table, whenever the child table gets updated or new records got inserted.
How could I do this?
What you are looking for is called a trigger.
Triggers are functions executed when an update insert or delete action takes place on a specified table.
Read more here
http://www.mysqltutorial.org/mysql-triggers.aspx

How to automatically run a query when a table is amended by INSERT/UPDATE/DELETE?

I have a query which basically "syncs" all the data from a table in one database, to a replicated table in another database.
Here is the simple query:
TRUNCATE TABLE [Database2].[dbo].[USER_SYNC]
INSERT INTO [Database2].[dbo].[USER_SYNC]
SELECT * FROM [Database1].[dbo].[USER]
Now, after some research, I had a look into using a trigger to do this, however, I read up that stored procedures and heavy queries such as this should not be used within a trigger.
Therefore, what is the best way in which I can automatically run this query from within SQL, whenever a record in database1 is inserted, amended or deleted?
And if what I read up about triggers was incorrect, then how would I go about creating one for my procedure? Thanks.
If you need to sync tables you do not need to truncate one every time on update, delete or insert.
Create identical copy of user table.
Create on update, on delete, on insert triggers on the original user table.
In the trigger update, delete or insert to the duplicate table only one row at a time - the one that was updated, deleted or inserted to the original user table. This will not be a heavy query.
UPDATE:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html