Query related to trigger fire in SQL database? - mysql

Is it possible to fire a trigger when a row value in the table equals a certain value? Like if a value in the table hits zero, I want a certain DML operation to occur using a trigger, is this possible?

Triggers fire based on events on DB objects. For example you can define a trigger to fire before or after insert, update or delete events on tables. What you do in the trigger implementation is entirely up to you, DB will make old record (for update and delete) and new record (for insert and update) available to you.
For your requirement, you can implement a logic that will check for the attribute's value and if it hits zero, you perform the DML operation else just return without doing anything.

Related

MySql After Update Trigger execution order

I am calling an API of my Nodejs app to update a record in my MySQL database.
I defined an "After Update trigger" on it. the trigger calls a post restful API using sys_exec,to pass the updated record's ID to another API. Then, the other API fetch the record and based on the updated values, will insert a new record in the other table of the same database.
But what actually happens is: first the second API insert new record based on the old values of the record and then the old value update new value.
As far as I know, "after update trigger" guarantees to start executing trigger after updating current record.
any suggestion or help, please.
The after update trigger runs after the record is updated, but before the committing of the transaction.
By calling another api from the trigger, the 2nd insert is most likely runs in a different transaction. Unless you change the isolation level to read uncommitted, the 2nd transaction can only read the committed, therefore unchanged values of the record.
I would do the 2nd insertion from the trigger, not from another api because the trigger can obviously see the updated values. The 2nd api can still take care of whatever else it is doing at the moment.
I would not recommend changing the isolation level to read uncommitted - unless you really know what you are doing. It can have unintended side effects.

mysql delete row if all null after update without trigger

For a certain table, if a field is updated to Null and all fields for that row are now Null I want to delete this row. I understand this can be done with a trigger, but I have heard that triggers should be avoided whenever possible. Is there another solution to this which avoids triggers, and has lower cost? How expensive would a trigger even be in this case (is it less expensive than running two queries?)
You have three options:
Put in a DELETE FROM TABLE WHERE FIELD ==NULL after every update.
Use a trigger
Centralize all updates through a stored procedure so that you can call the SP when you update the record, which can then call the DELETE FROM command in #1.
Triggers should not be avoided wherever possible. They should just be used with discretion.
First, in many systems that I've been involved with, all the DML operations are embedded in stored procedures. Basically, anything a trigger would do is in the stored procedure -- along with other functionality such as logging.
Second, if you have a problem that requires a trigger, then use a trigger. For instance, prior to Oracle 12C, you needed a trigger to add an auto-incrementing primary key. By all means do it, if that is what you need.
Don't go overboard. But your particular problem seems like an example where a trigger could be quite useful.
Do realize that there might be other solutions. For instance, perhaps a view would do what you want:
create view v_t as
select t.*
from t
where col1 is not null or col2 is not null or . . .;
This would save all the overhead of the trigger. If the table got too big, you could delete the all-NULL rows using a scheduled job.

SQL Server trigger not working while inserting values

Is it possible the insert trigger is not run while the values are inserted on the table? Anybody was experience this?
No it's not possible, if you have the trigger set up correctly. Are you sure you've indicated you want the trigger to fire on an Insert? You have the option of specifying a trigger to fire on an Insert, Update, Delete or any combination.
Or the trigger could be disabled. Or there could be a Return statement in the first line of the trigger. I've seen people do that, as a way to disable a trigger.
Also, if you are inserting multiple rows into the table, you need to make sure your trigger is created correctly to handle that. Handling multiple inserts can be a bit more complicated than handling a single insert, and unexpected results could occur if you are not aware of the difference.

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

sql : Enable and Disable Triggers

I have trigger on a table which is written longtime back and can’t retire or modify at this moment. There are lot of select statements are there which get fired irrespective of any condition in this trigger.
Now I have another Stored Procedure which will update the two columns in the above mentioned table and I don’t want any other operation or any queries which were written in the trigger needs to be fired when this operation ( calling SP) is performed.
So I though before I call the update statement in this stored procedure, I disable the update trigger on this table and once I done with update statement will again enable the trigger .
Is this is good idea ? Any issues with this approach? I will do this operation in transaction so that if anything goes wrong , it will come back to original stage .
You can disable/enable a trigger by hand.
It is a good idea, as long as you are sure that the trigger does not update some other field or table and if no other job launching that trigger may run at the same time.