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

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 ;

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

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|

Error when run query trigger in MySQL

please help
i have trigger code below
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$
after running it, it shows 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 17
how to fix it? MySQL version is 5.5.34
table is a reserved word in mysql. Surround table with backticks. Also you were missing delimiter and a semicolon at the end.
This works:
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END;$$
delimiter ;
I hope that helps!
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table <-- you should escape this using backticks as this is a reserved word i.e., `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$

MySQL trigger before update

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 ;

MySQL Trigger Syntax Error

Running on MySQL 5.5.9 with InnoDB.
I created the following trigger:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber
END;
MySQLWorkbench shows a syntax error on the last line, and executing this throws the following error
Error Code: 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' at line 11
Where is my error?
http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER |
CREATE TRIGGER TRIGGER_Products_Insert AFTER INSERT ON Products
FOR EACH ROW BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;
|
DELIMITER ;
You are missing a ; before your END keyword:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;