MySQL trigger before update - mysql

I want to add trigger in mysql ...
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN SET
NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END
And I always get error:
Error
SQL query: Documentation
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH
ROW
BEGIN SET NEW.`order` = ( SELECT 1 + COALESCE( (
SELECT MAX( `order` )
FROM `table_1` ) , 0 )
);
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
Any ideas?

You need to change the delimiter. Otherwise the engine thinks the statement is finished at the first ;. But that would be wrong.
delimiter |
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN
SET NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END
|
delimiter ;

Related

#1064 - You have an error in your SQL syntax at line 7

enter code here
DELIMITER $$
DROP TRIGGER IF EXISTS `Update_Status`$$
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`))
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET(`L_Seen`=new.`Cap_time`) WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`);
END$$
I have Created this After insert trigger On Occurrence _time I want to store first time of occurrence of a day and last time of occurrence of Day in Total_time table but getting this Error
"1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(L_Seen=new.Cap_time) WHERE (SSN=new.SSN and Day_Date=new.`Day_Date...' at line 7" ***
Instead of SET(L_Seen=new.Cap_time) Please use SET L_Seen=new.Cap_time. You missed end if also. Below code is working.
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`)
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET `L_Seen`=new.`Cap_time` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`;
end if;
end
DB-Fiddle:
create table occurance_time(SSN varchar(50),Name varchar(50),Day_Date date,Cap_time time);
CREATE TRIGGER Update_Status AFTER INSERT ON occurance_time
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT F_Seen FROM total_time WHERE SSN=new.SSN and Day_Date=new.Day_Date)
THEN
INSERT INTO total_time (SSN,Name,Day_Date,F_Seen) VALUES(new.SSN,new.Name,new.Day_Date,new.Cap_time);
ELSE
UPDATE total_time SET L_Seen=new.Cap_time WHERE SSN=new.SSN and Day_Date=new.Day_Date;
end if;
end
db<>fiddle here

How to update a table using trigger mysql

I have two tables, t1 and t2.
I'm trying to make a trigger so that when t1 gets an update, I check if what was updated was t1.nStatus.
If t1.nStatus = 2, I need to set t2.bEnabled = 1 for all t2.customer_Id that are equal to t1.nId
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END;
The error I got:
1 queries executed, 0 success, 1 errors, 0 warnings
Query: CREATE OR REPLACE trigger changeStatusImmobile after UPDATE on
immobile for each row begin IF NEW.nStatus = 2 then UPDATE select...
Error Code: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near '' at line 5
Execution Time : 0 sec Transfer Time : 0 sec Total Time : 0.146
sec
Have you some idea?
Try setting a delimiter
DELIMITER $$
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END$$
DELIMITER ;

MySQL insert query if condition matches in after insert trigger

I'm using WampServer with MySQL Version: 5.7.9 and phpMyAdmmin Version: 4.5.2
I'm trying to write an after insert trigger on Table1 to insert data in Table2 if the data doesn't exist in Table2. Below is my query:
DROP TRIGGER IF EXISTS `insert_customer`;
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;
This gives me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
I've tried with delimiter too:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`)
END IF;
END;
|
DELIMITER ;
This gives me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF;
END' at line 5
What am I doing wrong here? Any help?
Finally I've got it. The correct SQL is:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0 THEN
INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;|
DELIMITER ;
The ; at the end of my INSERT query solved my problem. :D
My guess is the extra delimiter; try changing
END;
|
to
END|

My Mysql do not know trigger, what can i do?

in the name of god.
hello. i want create a trigger in database on table but mysql do not know trigger.
i use of XAMPP 1.7.3(php 5.3.1,Mysql 5.1.41).
CREATE TRIGGER TRIGGER_NAME ON TABLE_NAME (FOR/BEFOR/AFTER) {INSERT,UPDATE,...}
...
please precision to trigger color on image :
CREATE TRIGGER `TR_INSERT`
BEFORE INSERT ON `tbl_users`
IF ( SELECT COUNT(*) FROM tbl_users WHERE username IN ( SELECT username FROM inserted))>1
begin
PRINT 'THIS NAME IS EXISTS'
ROLLBACK TRANSACTION
end
error : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF ( SELECT COUNT(*) FROM tbl_users WHERE username IN ( SELECT username FROM ins' at line 4
You could achive the same thing with an unique key on username. But for the sake of learing about triggers:
delimiter |
CREATE TRIGGER `TR_INSERT` BEFORE INSERT ON `tbl_users`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM tbl_users WHERE username IN (NEW.username)) > 1
THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'THIS NAME ALREADY EXISTS';
END IF;
END
|
delimiter ;

Trigger creation query failed

I have three tables, when data is inserted into third table, I am checked whether newly inserted row has field value in table1. If value exists then I am moving the same row from table3 to table2. My code is like below, But I am getting error while executing this
DELIMITER $$
CREATE TRIGGER onRosterAdd
AFTER INSERT ON yet_tobe
FOR EACH ROW
BEGIN
SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END
END$$
DELIMITER ;
And here is the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
How can I fix this.
Try it like this:
DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END IF;
END$$
DELIMITER ;
Here's a working fiddle