Just learning about triggers and I'm created the following trigger;
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where msisdn = new.msisdn order by dat DESC limit 1);
END;
However the value does not appear to be getting updated. Any ideas?
I've actually managed to solve this myself. Here is the updated code
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON `incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where crm_record.msisdn = new.msisdn order by dat DESC limit 1);
END;
I needed to specify the table name prior to the column value on line 5.
Looks like you have a typo.
You have entered incremental with a trailing backtick instead of enclosing it in backticks.
It is likely that your trigger is now bound to a table called incremental` which I am assuming does not exist.
Since you have ruled out the above. I see that the UPDATE keyword is missing. Add UPDATE table before your SET line. Replace table with the name of your table.
Related
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.
I've got two columns in the same table for my users: name-displayed and short-name.
name-displayed is populated with the full name of the user, for example "John Doe". In short-name, there is the short value, e.g. "john-doe" (essentially de-capitalized and hyphenated).
How would I amend the data in short-name based on the data in name-displayed? I'm sure I could use a self-join based on UPDATE, but I'm not sure how to implement a change in data across the columns.
Any help would be hugely appreciated!
You need to use the Lower and Replace functions for this.
See: Lower and Replace in the docs.
Update <table_name>
set `short-name` = REPLACE(LOWER(`name-displayed`), ' ','-')
where <conditions>;
In case you want this done automatically, you'll need to write a trigger as Walter_Ritzel suggests.
delimiter //
CREATE TRIGGER auto_set_short_name BEFORE INSERT ON account
FOR EACH ROW
BEGIN
SET NEW.`short-name` = REPLACE(LOWER(`name-displayed`), ' ','-');
END;//
delimiter ;
You could use triggers: Triggers
A trigger Before Insert/Update could solve that easily.
delimiter //
CREATE TRIGGER ins_sum BEFORE INSERT OR UPDATE ON table
FOR EACH ROW
begin
SET New.`short-name` = REPLACE(LOWER(NEW.`name-displayed`), ' ','-') ;
end;
//
Use backticks or this char: ```, to make sure the - is not interpreted as a minus sign.
update table a join table b on a.id = b.id
set a.short-name = b.name-displayed;
I understand you need to set name-displayed same as short-name,
if that is not the case
care to explain.
what you want to amend ??
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 have created a MySQL Trigger BEFORE INSERT on table name agent_mst as below
BEGIN
DECLARE max_id INT;
SET max_id=(SELECT MAX(agent_id_pk)+1 FROM `agent_mst`);
IF (max_id IS NULL) THEN
SET max_id=1;
END IF;
SET NEW.date_added=NOW(),
NEW.date_updated=NOW(),
NEW.agent_code = CONCAT('SDA', LPAD(max_id, 4,'0'));
END
So what it does is, every time we inset a record, it generates agent_code field value to something like SDA0001, SDA0002, SDA0003, ...
Now suppose I delete a record with code SDA0003 and insert new record, it will definitely generate the agent code as SDA0004. As it is taking the max_id and increasing it with 1. But here I want to get SDA0003 again. So that all agent_codes can stay in sequence. How to do that?
Thanks in advance.
you need to identify the first (smallest) missing id.
check out in this link, a nice way to do it in a select query:
Find mininum not used value in mysql table
To know next auto increment id try to run below query and check column "Auto_increment":
SHOW TABLE STATUS FROM DBName where name = 'tableName'
I've got a mysql table column that acts as a counter (int). Each time I update that column I want the field value to get plussed with 1.
So if it was 45 I want it to be 46.
How could I do that with SQL?
You can use a query such as this one :
update your_table
set your_column = your_column + 1
where identifier = X
Of course, up to you to replace the names of the table and the columns ;-)
And to make sure the condition in the where clause is OK ;-)
MySQL v5.0.2 or higher supports triggers, which are pieces of code that are executed, whenever an insert/update/delete operation is made on one of the rows.
for more information about triggers in MySQL check this link
Since you want it to happen each time, you could use a trigger:
delimiter //
create trigger increment_field before update on your_table
for each row
begin
if new.your_column <> old.your_column then
set new.your_column = new.your_column + 1
end if;
end;//
delimiter ;
I am not entirely sure I understand your question. The above solution will make it so whenever a row is updated such that your_column is given a different value, it will instead make your_column = new_value+1. The use of new and old keywords and the conditional used on each row should be changed to suit your needs.