Having a table with the name transitions, I want to change the values of all the rows after any update is made.
I'm using the following trigger, which changes only the ROW that I'm making the update to.
CREATE TRIGGER signaturetrigger BEFORE UPDATE ON `transactions` FOR EACH ROW
BEGIN
SET New.signature = '288';
END
I'm trying to change all the rows to signature = 288, how can I modify the trigger in order to archieve that? I thought that using FOR EACH ROW would be enough.
Thanks in advance.
You can use an after update trigger with an update statement:
CREATE TRIGGER signaturetrigger AFTER UPDATE ON `transactions`
FOR EACH ROW
BEGIN
UPDATE transactions
SET New.signature = '288';
END;
This does seem like a very strange thing to do, however.
Consider an alternative: just add an UpdatedAt column into the table and update the signature in that row. Then, when you want the most recent signature use:
select signature
from transactions
order by UpdatedAt desc
limit 1;
An index on transactions(UpdatedAt, signature) will make this quite speedy. And, the update will go much, much faster than updating all rows.
Related
I want to update a column in MySQL only if the row is updated. (meaning all the other values trigger mysql to update the row.)
It's a concatenated text-field, so automatic calculation using ON UPDATE in the table definition won't work.
A trigger won't work either since the value is not fixed.
If there is ON UPDATE in table definition or triggers, there must be a way to determine that in some expression, right?
(Of course, I could create/update a trigger every time after the update or do a second update based on automatic update timestamp (which I then need to select first...) but that's both not very effective nor elegant.)
What I'd love to do would be something like this:
UPDATE tbl
SET x = 1, y = 2
ON UPDATE ( z = CONCAT_WS(', ',z,'blah') );
I'm fairly new to triggers and have already tried searching for a solution to my question with little results. I want to update a single row's start time column whenever it's active column is set to 1.
I have two columns ACTIVE (number) and START_TIME (timestamp) in my_table. I would like to create a PL/SQL trigger that updates the START_TIME column to current_timestamp whenever an update statement has been applied to the ACTIVE column - setting it to 1.
So far I have only seen examples for inserting new rows or updating entire tables which isn't what I'm looking to do. I'd have thought there would be a fairly simple solution to my problem.
This is what I've got so far from other examples. I know the structure of my solution is poor and I'm asking for any input to modify my trigger to achieve my desired result.
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (my_table.ACTIVE = 1)
begin
insert my_table.start_time = current_timestamp;
end;
\
you can use like this .it may help you
write the update query instead of insert query
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (new.ACTIVE = 1)
begin
update my_table set start_time =current_timestamp;
end;
I think it should be a BEFORE UPDATE, not AFTER UPDATE, so it saves both changes with a single action. Then you don't need the INSERT or UPDATE statements. I also added the "OF active" clause, so it will only start this trigger if that column was updated, which may reduce the workload if other columns get updated.
CREATE OR REPLACE TRIGGER routine_active
BEFORE UPDATE OF active ON my_table
FOR EACH ROW
BEGIN
IF active = 1
THEN
:NEW.start_time = current_timestamp;
END IF;
END;
I want to UPDATE my row after it has been updated by the user.
Example if after the user selects 32, and Principal previously entered is > 1000. Then Update the 32 to 40.
CREATE TRIGGER updateusers_id AFTER UPDATE ON Table
FOR EACH ROW
BEGIN
IF(NEW.users_id = 32 AND Table.Principal > 50000) THEN
UPDATE loans SET users_id = 40;
END IF;
END
Think about it for a second and you'll realize the first problem: if what you actually need here is an AFTER update trigger... then what would a BEFORE update trigger do?
AFTER UPDATE means after the update query has already changed the row data. What you are actually looking to is BEFORE UPDATE -- you want to hijack the update in mid-stream, before the table data actually gets changed, and potentially modify the values that will actually be written to the database.
UPDATE loans SET users_id = 40;
There are two problems with this. First, you know what an UPDATE without WHERE does... right? It updates all the rows in the table. Luckily, the server didn't let you do that. But the solution is not to add a WHERE clause -- it's this:
IF(NEW.users_id = 32 AND NEW.Principal > 50000) THEN
SET NEW.users_id = 40;
END IF;
The NEW pseudotable contains the row data that the user has tried to cause the UPDATE (that fired the trigger) to write to the row. You are in a BEFORE UPDATE ... FOR EACH ROW trigger, so setting NEW values overrides anything the user tried to do... which appears to be what you are wanting to accomplish.
I recentely posted a question about a trigger able to prevent update of specific tables, then I asked in comments a solution to prevent update of speficic columns and rows. I managed to write a trigger that prevents update of specific columns, but i still cant figure how to limit this to a specific number of rows. So I thought I should open a new question in order to give this solution to the ones that need it and also to have a reply on my other problem, meaning limit this trigger to a specific # of rows. The following trigger works for columns:
DELIMITER ;;
CREATE TRIGGER my_trigger BEFORE UPDATE ON test_table FOR EACH ROW
IF (NEW.price != OLD.price OR NEW.name != OLD.name) THEN
UPDATE UPDATE_OF_TABLE1_IS_NOT_ALLOWED SET value='Update not allowed!';
END IF;;
DELIMITER ;
Is there any way to choose the # of rows hit by this trigger? lets say i want to select an interval of rows or specific rows numbers... any hint? thanks a lot.
I have a table: ID,name,count,varchar(255)
Now, what i'd like is to increase the "count" each time that row in the table is updated.
Of course, the easy way is to read first, get the value, increase by 1 in php, then update with the new value. BUT!
is there any quicker way to do it? is there a system in mysql that can do the ++ automatically? like autoincrement, but for a single entity on itself?
I see two options:
1.
Just add this logic to every update query
UPDATE `table` SET
`data` = 'new_data',
`update_counter` = `update_counter` + 1
WHERE `id` = 123
2.
Create a trigger that will do the work automatically:
CREATE TRIGGER trigger_name
AFTER UPDATE
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table`
SET `update_counter` = `update_counter` + 1
WHERE `id` = NEW.id
END
Create a trigger:
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Triggers are pieces of code that are "triggered" by the database on certain events. In your case, the event would be an update. Many RDBMS support triggers, so does MySQL. The advantage of using a trigger is that every piece of your PHP logic that updates this entity, will implicitly invoke the trigger logic, you don't have to remember that anymore, when you want to update your entity from a different piece of PHP logic.
you can look up at the trigger
or can do with the extra mysql query
update table set count=count+1 ;
UPDATE table SET name='new value', count=count+1 WHERE id=...
An SQL update can use fields in the record being updated as a source of data for the update itself.