Creating Last Modified SQL Trigger - mysql

I am trying to create a trigger to update last_modified field when the row is updated.
This is what i have tried:
USE `mydb`;
DELIMITER $$
DROP TRIGGER IF EXISTS mydb.products_AUPD$$
USE `mydb`$$
CREATE TRIGGER `products_AUPD` AFTER UPDATE ON products
FOR EACH ROW BEGIN
SET NEW.`last_modified` = NOW();
END;
$$
DELIMITER ;
The problem is MySQL workbench wont let me save it (I'm assuming that there is something wrong with it but i can't find out what)

Turns out the version of MySQL Workbench I was using(6.0xxx) had a bug in it which played havoc with triggers. I updated to 6.2xxx.
That was the first problem.
The second problem was that i was using the NEW keyword in an AFTER trigger which is not allowed:
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.
So my new trigger looks like this:
CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`products_BEFORE_UPDATE` BEFORE UPDATE ON `products` FOR EACH ROW
BEGIN
SET NEW.`last_modified` = NOW();
END;
Notice there is no USE or DELIMITER statement? That is because the new version of MySQL Workbench puts this in for you (including the end delimiter $$):
USE `mydb`;
DELIMITER $$
USE `mydb`$$
DROP TRIGGER IF EXISTS `mydb`.`products_BEFORE_INSERT` $$
USE `mydb`$$
CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`products_BEFORE_INSERT` BEFORE INSERT ON `products` FOR EACH ROW
BEGIN
SET NEW.`last_modified` = NOW();
END;$$

Related

mySQL8.0 trigger statements

I am trying to create as following given:
Create a trigger on the Company table after an update that sets a
company's budget to 0 if it is less than 0.
But I struggle with errors such as being unable to update, set the table. I know it is because of I need to change from 'after update' to 'before update'. But I cannot dismiss the expected code. What could I do?
The mySQL code I have is:
delimiter $$
drop procedure if exists after_company_update $$
create trigger after_company_update after update on Company
for each row begin
update Company set budget = 0 where budget < 0;
end $$
delimiter ;
Error: ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG: Can't update table 'Company' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Such a trigger should logically be done before the update rather than afterwards:
delimiter $$
drop trigger if exists before_company_update $$
create trigger before_company_update after update on Company
for each row
begin
if old.budget < 0 then
set new.budget := 0
end if;
end $$
delimiter ;
You can express this as an after update trigger:
create trigger after_company_update after update on Company
for each row
update Company
set budget = 0
where Company.Id = new.Id;
The syntax works, but the trigger doesn't. It returns the error:
Can't update table 'Company' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
You can see this here.
You can't update the whole table which is already used by the trigger but you can update the column which is been modified.
Assuming your table has Primary Key which is company_id
Like
update Company
set budget = 0
where company_id=new.company_id and budget < 0;
'In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated' - https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
A before trigger would work but I'm not sure that it would be appropriate to have a trigger at all - do you really want to do this every time an update occurs to company (or only on specific occasions) , for example would it be appropriate if you changed address(assuming address is in the company table)
delimiter $$
drop trigger if exists after_company_update $$
create trigger after_company_update BEFORE update on Company
for each row begin
set NEW.budget = 0 where OLD.budget < 0;
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

MySQL trigger not working. What could be the issue?

I need to update an existing table after insert on another table. This is what I have.
DELIMITER $$
CREATE TRIGGER `some_trigger`
AFTER INSERT ON `old_table`
FOR EACH ROW BEGIN
UPDATE `new_table` set `some_column` = new.`column`
WHERE `new_table`.id = new.id
END $$
DELIMITER;
Trigger definition successfully executed and trigger exists with
SQL_MODE - NO_ENGINE_SUBSTITUTION
DEFINER - root#%
Is there something horribly wrong with this?
Can you please confirm that the new.id i.e the id generated on old_table insertion is also present in the new table ?
Because the possible reason could be that in new table new.id is not present at the time on old_table insertion.

MySQL error code 1235

In MySQL I tried to define a trigger like this:
DELIMITER $$
CREATE TRIGGER vipInvite
AFTER INSERT ON meetings
FOR EACH ROW
BEGIN
IF(NOT EXISTS (SELECT * FROM participants
WHERE meetid = NEW.meetid AND pid ='vip'))
THEN
IF(EXISTS(SELECT * FROM meetings WHERE meetid = NEW.meetid AND slot > 16))
THEN
INSERT INTO participants(meetid, pid)
VALUES (NEW.meetid,(SELECT userid
FROM people WHERE people.group = 'tap' GROUP BY invite));
END IF;
END IF;
END $$
DELIMITER ;
Produces this error:
This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table.
Is there a way to work around this so I can define multiple triggers?
This error means you already have an AFTER INSERT trigger on meetings table.
If it is the same trigger (meaning vipInvite) that you created earlier and now you want to replace it then you need to drop it first
DROP TRIGGER vipInvite;
DELIMITER $$
CREATE TRIGGER vipInvite
...
END$$
DELIMITER ;
Now if you have some other trigger you have to merge code from both triggers into one, then drop existing trigger, and then create a new one.
To show the list of existing triggers use SHOW TRIGGERS.
SHOW TRIGGERS WHERE `table` = 'meetings';
How to reproduce this error in MySQL:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple
triggers with the same action time and event for one table'
Run the following queries:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
DELIMITER //
CREATE TRIGGER mytrigger2 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
If you want to hook more than one action onto the same event/table, you will have to cram all of it into one trigger. You could call many stored procedures like this:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
CALL fromulate_the_moobars(NEW.myid);
CALL its_peanut_butter_jelly_time(NEW.myname);
END//

MySQL Trigger Not Executing like it should

I wrote the trigger below, but when I insert into the table it doesn't capitalize the inserted word, like it is supposed to. (no errors either). Any possible solutions?
(I have an employee and an employee2 table, since I read you cant have the trigger in the table it is trying to update).
DELIMITER $$
DROP TRIGGER IF EXISTS mytrigger$$
CREATE TRIGGER mytrigger before INSERT ON employee FOR EACH ROW
BEGIN
update employee2
SET employee2.FName = CONCAT(UCASE(LEFT(employee2.FName, 1)), LCASE(SUBSTRING(employee2.FName, 2)));
END;
$$
DELIMITER ;
You can have triggers on the table you are trying to update, you just can't run UPDATE queries. Instead, you modify the data before it is inserted:
DELIMITER $$
DROP TRIGGER IF EXISTS mytrigger$$
CREATE TRIGGER mytrigger before INSERT ON employee FOR EACH ROW
BEGIN
SET NEW.FName = CONCAT(UCASE(LEFT(NEW.FName, 1)), LCASE(SUBSTRING(NEW.FName, 2)));
END;
$$
DELIMITER ;
The "NEW" keyword gives you access to the data that is going to be inserted.