Creating a trigger in mysql - mysql

I'm trying to create a trigger on a table after inserting, which gonna update another table.
this is the code I tried :
delimiter |
CREATE TRIGGER augmenter_quantite_article AFTER INSERT
ON LigneInterventaire
FOR EACH ROW BEGIN
DECLARE #qte AS INTEGER;
DECLARE #code AS INTEGER;
SELECT #qte = qteInv FROM INSERTED;
SELECT #code = codeArt FROM INSERTED;
UPDATE Article SET qteArt = qteArt + #qte WHERE codeArt = #code;
END;
|
delimiter ;
but I get this error message :
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 '#qte AS INTEGER; DECLARE #code AS INTEGER; SELECT #qte = qteInv
FROM INSERTED; S' at line 4

Try
CREATE TRIGGER augmenter_quantite_article
AFTER INSERT ON LigneInterventaire
FOR EACH ROW
UPDATE Article
SET qteArt = qteArt + NEW.qteInv
WHERE codeArt = NEW.codeArt;
Here is SQLFiddle demo.
MySql has somewhat limited trigger implementation. There are no virtual tables inserted and deleted per se as in SQL Server. Instead you can access old and new values of a row that is being inserted or updated with keywords OLD and NEW respectively.
Since it boiled down to only one UPDATE statement you no longer need to use BEGIN END block. Another plus - you don't need change DELIMITER either ;)

Related

How to Trigger in MySql

I want to create a Trigger in MySql I did it with SQL Server at school in my class, but for my project, my web hoster only give me MySql DataBase and if I want a SQL Server DB I need to buy a VPS and I can't afford it so i'll do with MySql, I read about MySql Trigger and I saw that they are really more hard to do than SQL Server Trigger.
First here is every article I read about trigger in MySql (To be sure to not be placed as a duplica):
https://www.mysqltutorial.org/mysql-triggers.aspx/
https://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx/
https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://stackoverflow.com/questions/38745441/how-to-write-trigger-with-multiple-condition-in-mysql
https://stackoverflow.com/questions/33775826/how-to-declare-variables-in-trigger-with-mysql
And here is the SQL Server code I made:
CREATE TRIGGER file_verified_if_admin ON tblOpenSource
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE #source_id INT;
DECLARE #verified TEXT;
DECLARE #client_id INT;
DECLARE c_info CURSOR FOR
SELECT source_id, verified, client_id FROM inserted;
OPEN c_info;
FETCH c_info INTO #source_id,#verified,#client_id;
WHILE (##FETCH_STATUS=0)
BEGIN
IF ((SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblOpenSource.client_id = tblClients.client_id;) == 'admin')
BEGIN
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = #source_id;
END;
END;
CLOSE c_info;
DEALLOCATE c_info;
END;
Here is what I made with MySql and I still have errors, can someone help me ?
CREATE TRIGGER file_verified_if_admin
AFTER INSERT, UPDATE ON tblOpenSource
FOR EACH ROW
BEGIN
DECLARE client_grade text;
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin')
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = NEW.source_id;
END;
It says I have an error near my DELCARE, but I don't understand why, because I heard we don't need an "#" in the declaration.
What it is supposed to do, is to look if the file as been publied by an admin and if yes, it should update the verified column and change the text for verified
Mysql desn_'t allow multiple trigger in one goal you have to define then all.
Maysql needs DELIMITERto know where the trigger starts and ends
Last the IÌF CLause`is in mysql different.
This is the correct syntax, you can change it further by using SELECT INTO
UPDATE #Akina poited out,
that your query will not run, you can't change a row that has triggert the Trigger, mysql will not not allow that, so for your insert trigger, has to be change accordingly.
For the UPDATE Trigger goes teh saem.
DELIMITER //
CREATE TRIGGER file_verified_if_admin
BEFORE INSERT ON tblOpenSource
FOR EACH ROW
BEGIN
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin') then
SET NEW.verified = 'verified';
END IF;
END//
DELIMITER ;

How to declare variable in triggers

Upon creating a trigger i get the following 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 '' at line 3
This is my query:
CREATE TRIGGER reserved BEFORE INSERT ON Reserv
FOR EACH ROW BEGIN
DECLARE trip_id integer;
DECLARE free integer;
DECLARE count integer;
set #free := (select place_free from Trips where id = trip_id);
set #count := (select count from inserted);
if #count <= #free
begin
update Trips set place_free = #free - #count where id = #trip_id;
end
ELSE
BEGIN
ROLLBACK;
return;
END
END
What I see is that you are using the RETURN statement. As https://dev.mysql.com/doc/refman/5.7/en/stored-program-restrictions.html#stored-routines-trigger-restrictions states:
The RETURN statement is not permitted in triggers, which cannot return
a value. To exit a trigger immediately, use the LEAVE statement.
But from my point of view this is a different topic, as the error message speaks of a syntax error in line 3. Maybe you forgot to set the delimiter to something different than ;:
DELIMITER //
CREATE TRIGGER ...

MySQL #1064: declare a variable

I need to declare the variable in MySQL. and here is what I have till now.
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE my_cursor CURSOR FOR SELECT name FROM messages;
END
logically, it seams OK. but there is a strange error
i.e
SQL query:
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE my_cursor CURSOR FOR SELECT name FROM messages;
MySQL said:
#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
This works. Will gladly delete it when it gets dupe closed.
It merely lacks a delimiter wrapping block.
drop trigger if exists upd;
delimiter $$
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE vapenid INT;
END;
$$
delimiter ;

MySQL Delcare causing error

I am trying to create a trigger that marks items as deleted when they are inserted into the database.
Sadly I can't get my DECLARE to stop erroring, I have looked at the DECLARE docs and also at a few examples but I must be missing something.
The query I have so far is:
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END
The error message is showing:
#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
Thanks for your help and preventing me from defenestrating myself!
Try this:
DELIMITER $$
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END$$
DELIMITER ;
You need to change the delimiter when you create TRIGGER or STORED PROCEDURE.
By default, MySQL itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause MySQL to pass the entire stored program definition to the server. Otherwise, MySQL breaks CREATE TRIGGER, before it reaches the END statement (on the first semicolon, which, in your case, is DECLARE statement).
You can see the documentation for more details:
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.html

MySQL Trigger ERROR in phpAdmin

Hi
Is there any error in this TRIGGER Statement.When ever i try to run this in phpAdmin its giving error saying "#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 "SELECT Count(*) into SIM_CCode_Count".I cant get what's wrong in this..please help me
This is my trigger statement
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF
If NEW.SimCntISO<>NEW.NetCntISO then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF
END IF
END
Without proper explanation about your requirement and about tables and what you are expecting this trigger to do,its very difficult to say if any issues there in your trigger..
But as far as i can see there is some minor correction need to be done..
Try this Code and let know in detail your requirements..
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF;
If (NEW.SimCntISO<>NEW.NetCntISO) then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO;
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF;
End IF;
END;
You have to declare a mysql-statement delimiter before the trigger statement:
DELIMITER |
CREATE TRIGGER ...
(your code)
END|
DELIMITER ;
Otherwise MySQL interprets your ; in this statement as statement commit and executes the code immidiately. With the delimiter changed to a different character you can use the semicolon inside the trigger declaration safely.
See here: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html