MYSQL : Manual simulation of the triggering event - mysql

MYSQL:
I have a table with a AFTER INSERT ON trigger on it.
I have inserted a million rows in the table and the trigger code successfully gets executed and updates 10 other tables.
Now, I have deleted the data from the 10 children tables but the million rows in the parent table is retained.
Can I manually simulate that trigger code for those million records in the parent table to reinsert them in the 10 children tables...???

No you can't. Only actual, successful INSERTs trigger before/after insert triggers.

You can use the "AFTER INSERT" trigger like an "AFTER UPDATE", then you can make a safe update where you don't harm anything, this way you can re insert the rows.
Don't forget to delete "AFTER UPDATE", and restore de respective "AFTER INSERT".

Related

Trigger on MySQL table when multiple rows deleted

I wrote a trigger that runs before a row is deleted that updates a table summarizing the data from this table. The trigger works well when I delete a single row at a time. However, if I were to delete multiple rows at once with a statement like
DELETE FROM myTable WHERE id BETWEEN 1 and 100;
Will the trigger completely run on the first row before the next row is deleted or will the triggers run all at the same time?
The trigger will completely run for every single row, see Trigger FAQ
A.5.3: Does MySQL 5.6 have statement-level or row-level triggers?
In MySQL 5.6, all triggers are FOR EACH ROW—that is, the trigger is
activated for each row that is inserted, updated, or deleted. MySQL
5.6 does not support triggers using FOR EACH STATEMENT.
That is valid for the currently newest version, MySQL 5.7 too.
from http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
The statement following FOR EACH ROW defines the trigger body; that is, the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering event. In the example, the trigger body is a simple SET that accumulates into a user variable the values inserted into the amount column. The statement refers to the column as NEW.amount which means “the value of the amount column to be inserted into the new row.”
The delete transaction will only occur once, but then for every row affected by query the trigger will occur

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

How do i fire a trigger while truncating a table?

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

MySQL Trigger with transaction insert or update

i'm using mysql db & java, so in my application i need to use trigger, but i need to make it save while inserting or updating.
when insering new record in table A, trigger will do the work to insert some information in another table B.
also when doing update for records in table A, trigger will update records for table B.
if transaction during update or insrt rolledback, does this rolled back any changes done by the trigger.??!!
Yes, if the transaction is rolled back, the work done by the trigger will also be rolled back (unless you do some monkeying with transaction scoping inside the trigger to specifically prevent this)

SQL trigger to delete rows from database

I have an industrial system that logs alarms to a remotely hosted MySQL database. The industrial system inserts a new row whenever a property of the alarm changes (such as the time the alarm was activated, acknowledged or switched off) into a table named 'alarms'.
I don't want multiple records for each alarm, so I have set up two database triggers. The first trigger mirrors each new record to a second table, creating/updating rows as required. The second table ('alarm_display') has the 'Tag' column set as the primary key. The 'alarm' table has no primary key. The code for this trigger is:
CREATE TRIGGER `mirror_alarms` BEFORE INSERT ON `alarms`
FOR EACH ROW
INSERT INTO `alarm_display` (Tag,...,OffTime)
VALUES (new.Tag,...,new.OffTime)
ON DUPLICATE KEY UPDATE OnDate=new.OnDate,...,OffTime=new.OffTime
The second trigger should execute after the first and (ideally) delete all rows from the alarms table. (I used the Tag property of the alarm because the Tag property never changes, although I suspect I could just use a 'DELETE FROM alarms WHERE 1' statement to the same effect).
CREATE TRIGGER `remove_alarms` AFTER INSERT ON `alarms`
FOR EACH ROW DELETE FROM alarms WHERE Tag=new.Tag
My problem is that the second trigger doesn't appear to run, or if it does, the second trigger doesn't delete any rows from the database.
So here's the question: why does my second trigger not do what I expect it to do?
The explanation can be read here: http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html
Within a stored function or trigger,
it is not permitted to modify a table
that is already being used (for
reading or writing) by the statement
that invoked the function or trigger.
This is your problem and your trigger ends with error #1442.
The table alarms is already being used by the statement that invoked your trigger (the insert). This essentially means you cannot modify alarms with a delete trigger.
Cheers!