How to set a trigger which update a field after insertion - mysql

I have a table called list which has a id,name and size. Size stores the length of the name field. I want to update this field after insertion. In other words, Trigger after insert ( yes I can do in the code but I just curios about it) Any suggestions?

You should use a BEFORE INSERT trigger. Use the keyword new to refer to the columns about to be inserted:
CREATE TRIGGER my_trigger BEFORE INSERT
ON list FOR EACH ROW BEGIN
SET NEW.size = LENGTH(NEW.name)

Related

SQL trigger for add some data

Thank you for your help.
I have 3 tables on MySQL. One Table for my users and second is for my user features, the third one is for roles. First of all, I need to add registration on a measures table when the user is inserted. I mean if I add a new user to tbl_users the trigger will grab the user id and write on the tbl_measures that's it. I need just this.
INSERT INTO tbl_measures (user_id) VALUES (tbl_users.id)
this my triggers it is working partly. when I add a new user trigger add a new line to tbl_measures without id.
When you refer NEW.id in your insert trigger, there are two main cases. In the first case you get the proper id value. This is what you expect. In the second case it's null. This is what happens.
Essential is whether the trigger runs before or after the insert. Before the insert the id does not exist, because the record was not created yet. After the insert the id exists. Long story short code:
DELIMITER //
CREATE TRIGGER PROOFOFCONCEPT
AFTER INSERT
ON EACH ROW
BEGIN
INSERT INTO tbl_measures(user_id)
VALUES (NEW.id);
END; //
DELIMITER ;
I found the solution:
CREATE TRIGGER `after_members_insert` AFTER INSERT ON `tbl_users`
FOR EACH ROW BEGIN
INSERT INTO tbl_measures(user_id)
VALUES(new.id);
END

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!

MYSQL Trigger to insert column value in same table

I have a mysql Innodb table 'classrooms_subjects' as
id|classroom_id|subject_id
classroom_id & subject_id are composite keys. Whenever i insert a row with classroom_id & subject_id, my id field is inserted as 0.
Now i want to create a trigger which will enter id field as last_inserted_id()+1.
Also I need to take care of multiple records inserted at a time. My trigger is like below:
CREATE TRIGGER `increment_id` AFTER INSERT ON `classrooms_subjects`
FOR EACH ROW BEGIN
UPDATE classrooms_subjects
SET classrooms_subjects.id = LAST_INSERT_ID() + 1 WHERE id=0;
END
when i am inserting a record I am getting the error as:
"Cant update table in trigger because it is already used by statement which invoked this trigger
For general info: using an update statement inside the trigger isn't right.
Better to use a before insert trigger and simply assign the value of your column using NEW.id
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
A column named with OLD is read only. You can refer to it (if you have
the SELECT privilege), but not modify it. You can refer to a column
named with NEW if you have the SELECT privilege for it. In a BEFORE
trigger, you can also change its value with SET NEW.col_name = value
if you have the UPDATE privilege for it. This means you can use a
trigger to modify the values to be inserted into a new row or used to
update a row. (Such a SET statement has no effect in an AFTER
trigger because the row change will have already occurred.)
You should probably structure your table to make the auto_increment work properly. Better a solution that works when multiple sessions are inserting to the DB at once.

MySQL trigger INSERT all NEW columns

Is it possible to trigger the insert of all the columns for the NEW row? Basically, I want to make a copy of the newly insert record in another table but what having to specify all the columns.
Something like
DELIMITER $$
CREATE TRIGGER TestTrigger
AFTER INSERT
ON Table1 FOR EACH ROW
BEGIN
INSERT INTO `Table2` SELECT * FROM NEW;
END$$
DELIMITER ;
However this returns Table 'Database.NEW' doesn't exist whenever I try to insert a new row in Table1.
From MySQL documentation:
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. OLD.col_name refers to a column of an existing row before it is updated or deleted. NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.
Despite the time this question have been unanswered, you can SELECT all fields using primary key in source table if you have one.
CREATE TRIGGER replicated_table AFTER INSERT ON source_table FOR EACH ROW
BEGIN
INSERT INTO replicated_table SELECT * FROM source_table WHERE id=NEW.id;
END
Also, maybe you can prefer the use of REPLACE instead of INSERT to ensure the table will not stay out of sincronization.

How can I Update column value to Old value plus New value from other table using Trigger?

How can I Update column value to Old value plus New value from other table using Trigger if that value has already have an entry?
What I wanted is something like the following. Notice the bold and italicized part.
DELIMITER$$
CREATE TRIGGER trigger_name AFTER INSERT
ON table_one FOR EACH ROW
BEGIN
INSERT INTO table_two(clmn_id, clmn_one) VALUES(NEW.clmn_id_fk,NEW.clmn_a)
ON DUPLICATE KEY UPDATE clmn_one = VALUES(clmn_one + NEW.clmn_a);
END$$
DELIMITER;
Try removing the keyword VALUES from the ON DUPLICATE KEY:
DELIMITER$$
CREATE TRIGGER trigger_name AFTER INSERT
ON table_one FOR EACH ROW
BEGIN
INSERT INTO table_two(clmn_id, clmn_one) VALUES(NEW.clmn_id_fk,NEW.clmn_a)
ON DUPLICATE KEY UPDATE fine_amount = clmn_one + NEW.clmn_a;
END$$
DELIMITER;
Looks like you need a select statement first, to check if it already exists. If so, set variables to current values, then run an update that combines the old values (variables) and new values. If the record doesn't already exist, run insert statement with current values.