I'have a problem trying to create a trigger. I have table_1 and table_2, the update event on table_1 should update a row in table_2 identified using the where clause:
CREATE DEFINER=`root`#`localhost` TRIGGER `update_table_2` AFTER UPDATE ON `table_1` FOR EACH ROW BEGIN
IF table_1.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;
END
What's my problem?!
Thanks
In a trigger, you should use the old and new keywords to reference the record that has been updated (the old values and new ones respectively).
In your case, you should do :
IF new.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;
Related
I'm new with MySQL. I have this trigger:
DROP TRIGGER IF EXISTS `increment`;
TRIGGER `increment`
AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
UPDATE table2
SET table2.mycolumn = table2.mycolumn+1
WHERE table2.id = table1.id;
END
It gives error:
Unknown column 'table1.id'
Why? (The column exists). If I use this the trigger works fine:
WHERE table2.id = 1;
Help me understand this, please.
Use NEW or OLD to refer to the columns of the row that caused the trigger to be executed. But not columns of a table you subsequently change in a SQL statement in the body of the trigger.
Example:
TRIGGER `increment`
AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
UPDATE table2
SET table2.mycolumn = table2.mycolumn+1
WHERE table2.id = NEW.id;
END
In this example, NEW.id refers to the id column of the current row inserted to the increment table.
There is no OLD version of the current row in an insert trigger, because it's a new row.
So I do have a problem of understanding here. I would really appreciate if you help to clear my confusion.
So i have 2 tables table_1 and table_2 , where both the tables have the same columns as 'id' pk auto increment and 'name' varchar(20) .
Lets say i already have an after insert trigger on table_1 where everything i insert something in table_1 it stores the same copy in table_2 as well.
Now i wanted to apply an update trigger on the same table (table_1) to control if someone tries to modify the name in table_1 , it should take the name from table_2 and insert the correct name back to table_1.
I hope i explained it well. I will share the after update trigger i made which did not work. I would be more than happy if you help. Thank u
delimiter $$
CREATE TRIGGER `Practice`.`table_1_after_UPDATE` AFTER UPDATE ON `table_1` FOR EACH ROW
BEGIN
if old.name != new.name then
update table_1
set name = table_2.name
where id = table_2.id;
end if;
END$$
delimiter ;
CREATE TRIGGER `Practice`.`table_1_BEFORE_UPDATE`
BEFORE UPDATE
ON `table_1`
FOR EACH ROW
set NEW.name = OLD.name;
I have two tables for example table1 and table2. If something is deleted in table1 i want to update a column in table2. Is this even possible with a trigger in phpmyadmin? if yes what do i have to add or which syntax i have to use for it to work?
I tried this so far:
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
-- this is the part i dont know what to do and i couldnt find any related to my task
END//
DELIMITER ;
Well, in your TRIGGER you can access the value you just deleted with OLD.your_column_name.
So just do :
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
UPDATE table2 SET column_name = your_new_value WHERE column_name = OLD.old_value;
END//
DELIMITER ;
I want to insert the last updated row into a new table using trigger ? How to fetch that row if any of the column (value ) from that table has been updated or changed ?
i am using this code
CREATE TRIGGER `database`.`tbl1_AFTER_UPDATE`
AFTER UPDATE
ON `tbl1`
FOR EACH ROW
BEGIN
INSERT INTO tbl2 (c1,c2,c3)
SELECT
c1,c2,c3 from tbl1;
END
But this is inserting all the rows from tbl1 into tbl2, i want only the updated rows to be inserted into tbl2
Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.Trigger
DELIMITER $$
CREATE TRIGGER `database`.`tbl1_AFTER_UPDATE`
AFTER UPDATE
ON `tbl1`
FOR EACH ROW
BEGIN
INSERT INTO tbl2 (c1,c2,c3) VALUES (OLD.c1,OLD.c2,OLD.c3);
END $$
DELIMITER ;
On MySQL 5.5, I am trying to write a trigger to do the following:
Checks a condition for the insert (is group_id between 8-20?)
If the condition is true, then update the user with the same user_id on another table, called phpbb_users.
The update on this other table is to set user_rank to NEW.group_id from the insert on the table that has the trigger.
Else, do nothing.
Not experienced at all, and stuck. It's not working when I run the query. Here is what I've got so far.
DELIMITER $$
CREATE OR REPLACE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW BEGIN
IF (NEW.group_id BETWEEN 8 AND 20) THEN
UPDATE phpbb_users
SET user_rank=NEW.group_id
WHERE user_id=NEW.user_id;
ELSE
UPDATE phpbb_users
SET user_rank='0'
WHERE user_id=NEW.user_id;
END IF;
END$$;
DELIMITER;
(Note, the "else" clause is unnecessary really - happy for any suggestions for improvement of this code!)
if you only want to get rid of the else try
CREATE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW
UPDATE phpbb_users
SET user_rank=IF(NEW.group_id BETWEEN 8 AND 20,NEW.group_id,"0")
WHERE user_id=NEW.user_id;