Firing a MySQL trigger on existing table upon creation of trigger - mysql

I have a table with 2 columns named "Table1":
(Column 1 named "Col1" with the values: A,B,C,D,E,F)
(Column 2 named "Col2" with the values: 12,15,2,5,200,1).
I would like get all the values from column 2 to change to the value 1 if their value is lower than 100, so that column 2 will eventually look like this:
(Column 2 named "Col2" with the values: 1,1,1,1,200,1).
I tried to create a trigger:
delimiter //
CREATE TRIGGER Table1upd BEFORE UPDATE ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
When creating the trigger in MySQL workbench it says that the trigger was added but that 0 row(s) affected.
I assume my problem is with the BEFORE UPDATE choice, because the trigger does work when I update a value in the table, but I don't know what to change it to so that the trigger will also initially execute automatically when I create it.
Thank you in advance for any help,
D

The answer is simple: a BEFORE UPDATE trigger only triggers on UPDATE, so you need a second trigger BEFORE INSERT to cover both use cases: update and insert:
delimiter //
CREATE TRIGGER Table1insert BEFORE INSERT ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;

Related

MySQL Trigger to Update a field in one table after inserting into another

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!

how to sum two columns and insert into total column automatically after every update

I have three columns column 1, column 2, column 3(total). i want to put the sum of column 1 and column 2 into column 3 after every update query..... note i am using code igniter.. how can i do it if i update col 1 then total column automatically update itself.
Try like this:
DELIMITER $$
CREATE TRIGGER updtrigger AFTER UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 <> OLD.col1 OR NEW.col2 <> OLD.col2 THEN
SET NEW.col3 = NEW.col1 + New.col2;
END IF;
END $$
Here is a very good tutorial.
An AFTER UPDATE Trigger means that MySQL will fire this trigger after
the UPDATE operation is executed.
why dont you just update the db instead of creating a trigger. plus youve created the trigger after the insert, it should go before. but really you shouldn't be doing it that way
try this code:
DELIMITER $$
CREATE
TRIGGER `db_name`.`trigger_name` AFTER UPDATE
ON `db_name`.`table_name`
FOR EACH ROW BEGIN
UPDATE
`table_name`
SET
`column3` = `column1` + `column2`;
END$$
DELIMITER ;

mysql trigger to update the same table from another table

I'm trying to set up a trigger which will update same row by inserting some additional data from another table. Field receive.iccid is blank, and I want it to be updated on every insert. However this trigger doesn't work
delimiter //
CREATE TRIGGER ins_iccid
AFTER INSERT ON receive
FOR EACH ROW
BEGIN
UPDATE receive SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END//
delimiter ;
turns out that I need to use 'BEFORE INSERT' to achieve what i was trying to
CREATE TRIGGER `ins_iccid` BEFORE INSERT ON `receive`
FOR EACH ROW
BEGIN
SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END

Make a trigger that copies new data from table to another table after insert

I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.

MySQL trigger after update on the same table

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 ;