I want to automatically copy data from one column to another in one table.
The following query does this, but not automatically:
UPDATE table SET columnB = columnA
Could someone show a trigger for this (that does it automatically)?
Note: columnB and columnA are integers.
Should be as simple as:
delimiter //
CREATE TRIGGER before_table_update BEFORE UPDATE ON table
FOR EACH ROW
BEGIN
SET new.columnB = new.columnA;
END //
delimiter ;
Be aware, this will only effect UPDATE's. You will need to copy this trigger for inserts as well before_table_insert BEFORE INSERT...
Related
Tables
I have the two tables above. In tblUniformAndMaterials the field AllocatedMaterials is populated from a drop down list which is fed by from tblMaterials. Once selected the field MaterialID in tblUniformAndMaterials is Auto Populated from tblMaterials.
What I want to accomplish by using a Trigger is after the record in tblUniformAndMaterials is inserted I want to update the NiveauDeStock field in tblMaterials to (NiveauDeStock-1). In other words after each material allocation I reduce the stock level by one.
Reference for CREATE TRIGGER https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
Try this:
DELIMITER $$
CREATE TRIGGER tblUniformAndMaterials_ai
AFTER INSERT
ON tblUniformAndMaterials
FOR EACH ROW
BEGIN
UPDATE tblMaterials
set NiveauDeStock = NiveauDeStock -1
WHERE CodeDeMaterial = NEW.MaterialID;
END;
$$
DELIMITER ;
Note AFTER INSERT and how NEW.MaterialID is the ID inserted into tblUniformAndMaterials.
Good Luck!
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 am trying to write a MySQL trigger. I have two tables that look like this:
When a customer makes a purchase a new record is added to each table. I have added column ‘sku_copy’ to Table B, so it does not get populated when a new record is created.
When a new record is created, I want my trigger to copy the ‘sku’ field in Table A to the ‘sku_copy’ field in Table B. However, the problem I am having is how to structure the following condition in the trigger.
IF: ‘order_id’ in Table A matches ‘order_id’ in Table B. THEN: copy ‘sku’ from that Table A record to the record in Table B with the matching ‘order_id’. The data should be added to Table B ‘sku_copy'.
ELSE: don’t do anything.
Can someone show me how to write this into my trigger?
Thanks for any help you can give.
Try this but why do u think of using trigger for this ?
DELIMITER $$
CREATE TRIGGER trigger_name
AFTER INSERT ON tableA
FOR EACH ROW BEGIN
INSERT INTO tableB
SET sku_copy = OLD.sku,
order_id = OLD.order_id,
order = OLD.order;
END$$
DELIMITER ;
I want to post the trigger that was constructed with examples offered here and on another forum. Thanks to everyone who helped with this.
DELIMITER $$
CREATE TRIGGER sku_AfterInsert
AFTER INSERT ON uau3h_virtuemart_order_items
FOR EACH ROW BEGIN
UPDATE uau3h_virtuemart_orders
SET order_item_sku_copy = NEW.order_item_sku
WHERE virtuemart_order_id = NEW.virtuemart_order_id;
END$$;
DELIMITER ;
I have a MySQL trigger using the BEFORE INSERT ON table that calculates a value and updates the same table after a user inserts values in specific columns. This works as expected. But a user makes a mistake in their entry and fixes their error and I want to write a trigger that will update the calculated value after the error has been fixed. Is there a way to achieve this?
A BEFORE UPDATE ON table trigger has access to the existing values in the row as well as newly supplied values, and can set the value of any column in the table, based on whatever conditions and expressions we want.
For example, it's possible to test whether the value of one or more columns of concern has been modified, and then set some other column to some expression.
DELIMITER $$
CREATE TRIGGER my_before_update_trigger
BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
IF NOT ((NEW.col1 <=> OLD.col1) AND (NEW.col2 <=> OLD.col2)) THEN
SET NEW.col3 = NEW.col1 * NEW.col2 ;
END IF;
END$$
DELIMITER ;
I'm trying to create a trigger that will do two things:
Copy everything from a row in table X to a row in table Y before an UPDATE is done
Copy one column from a row in table X to a row in table Y after the UPDATE from no. 1 is done
This is what I have until now:
delimiter //
create trigger log
before update on opnaar
for each ROW
begin
set #a = opnaar.id;
set #c = opnaar.initials;
set #d = opnaar.revised;
set #f = opnaar.course;
insert into log(id,init_old,date_old,date_new,course) values (#a,#c,#d,NOW(),#f);
end;
after update on opnaar
for each ROW
begin
set #e = opnaar.initials;
insert into log(init_new) values (#e);
end;
But I figured it's not possible to add just one value to an already existing row in a table. At least, not the way I am trying to do this. Should I place the after update within the first action?
Example: I have this content:
I want to copy course, revised, initials and id to another table. That's what my first action should do.
After a teacher has updated something in that table, the NEW initials (but in the same column as the old initials) should be copied to table Y. That's what my 2nd action should do.
What am I doing wrong?
Thank you.
Have a look at the CREATE TRIGGER syntax. It is not possible to specify AFTER and BEFORE option in one trigger.
So, you should create two separate triggers.
If you want to optimize code and combine trigger's bodies, then you can write a stored proceure and call it from the triggers.