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
Related
I have an Access database and one of my tables is connected a form. I want to delete a record from my table and add it to another table with just one button. Deleting it is simple but I can't find a way to add the record to another table. Do you have any opinion on that?
1 Create 2 queries
An insert query
A delete query
2 Create a macro that has three actions
Open query( To run an append query)
Open query(To run a delete query)
A msg box, that will pop up to state that the two queries above ran
successfully
N:B
The insert and delete query will have parameters like record id
Attached is sample of insert query with parameter
The insert query in the macro condition should come first
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?
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
I have two tables:
tableOriginal
tableBackup
They have exactly the same structure.
I want a SQL statement I can run anytime of the day, that will copy all the rows from tableOriginal to tableBackup WITHOUT overwriting items in tableBackup. Basically, this command must synchronize tableBackup with tableOriginal.
How do I do that?
INSERT INTO tableBackup(SELECT * FROM tableOriginal)
As long as there is no issue with primary keys being updated or replaced with new incoming data this should not create an issue for you. However as you already know, backup table will have more data after your command since it did not delete previous data it had
Why don't you delete first all the data in tableBackup, then INSERT the data in tableOriginal to tableBackup
DELETE FROM tableBackup
INSERT INTO tableBackup(SELECT * FROM tableOriginal)
Why do we need to delete first?
Because if we're going to insert unique data into the tableBackup,
next time we insert it will not execute, because we will insert/add some data that is already been there..
Hope you get what I'm trying to say.
I am using MS SQL server 2008. Right now am firing my trigger on deleting records from my Master table to delete corresponding records from my child table. Now am trying to fire the trigger on truncate on my Master table. Is it possible? If yes, kindly help me to find the solution.
TRUNCATE TABLE cannot activate a trigger because the operation does
not log individual row deletions.
Ref.
Suggest you perform an actual delete, and perform in batches if this was the original reason (i.e. locking) you used TRUNCATE instead of DELETE