MySQL Error on Before Trigger for END IF Statement - mysql

Hello I am trying to write a trigger in mysql and below is my code`
DELIMITER $$
CREATE TRIGGER updateNewEmp
BEFORE INSERT ON employess
FOR EACH ROW
BEGIN
IF new.salary <50000 THEN
SIGNAL SQLSTATE '45000'
END IF
END$$
DELIMITER ;
But I get an error stating #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 'END IF END' at line 7. Could someone please explain what am I doing wrong here?

Try putting semicolons in:
DELIMITER $$
CREATE TRIGGER updateNewEmp
BEFORE INSERT ON employess
FOR EACH ROW
BEGIN
IF new.salary <50000 THEN
SIGNAL SQLSTATE '45000';
END IF;
END$$
DELIMITER ;

Related

MariaDB: Before Insert trigger to conditionally abort writing a new record

I'm attempting to create a trigger to abort writing a new record for a library database if the book in question has already been loaned out. Here's what I have so far:
CREATE TRIGGER OnlyOneBorrowerAtATime
BEFORE INSERT ON Transaction
FOR EACH ROW
BEGIN
IF (EXISTS ( SELECT * FROM Transaction
WHERE NEW.bookId = Transaction.bookId AND Transaction.transType = 'Loaned'))
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Book has not been returned';
END IF;
END;
I get the following error back from MariaDB:
SQL 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 '' at line 9
MariaDB version is 10.5.9.
Add a DELIMITER to your query like
DELIMITER $$
CREATE TRIGGER OnlyOneBorrowerAtATime
BEFORE INSERT ON Transaction
FOR EACH ROW
BEGIN
IF (EXISTS ( SELECT * FROM Transaction
WHERE NEW.bookId = Transaction.bookId AND Transaction.transType = 'Loaned'))
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Book has not been returned';
END IF;
END$$
DELIMITER ;

I am getting a syntax error when creating a MySQL trigger

I try to create a trigger but I get this error after executing the SQL:
# 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 'DECLARE temp INT' at line 4
Here is my trigger:
CREATE TRIGGER hesap
AFTER INSERT ON uber
DECLARE temp INT;
BEGIN IF uber.il='ANKARA' THEN
temp=5+(uber.mesafe*0.5);
ELSEIF uber.il='ISTANBUL' THEN
temp=10+(uber.mesafe*0.5);
ELSEIF uber.il='IZMIR' THEN
temp=3+(uber.mesafe*0.5);
END IF;
INSERT INTO fatura VALUES(uber.uid,temp)
END;
There is some syntax error as I identified check out the below code:
delimiter $$
CREATE TRIGGER hesap
AFTER INSERT ON uber FOR EACH ROW
BEGIN
DECLARE temp integer;
IF new.il = 'ANKARA' THEN
temp= 5+(uber.mesafe*0.5);
ELSEIF new.il='ISTANBUL' THEN
temp=10+(uber.mesafe*0.5);
ELSEIF new.il='IZMIR' THEN
temp=3+(uber.mesafe*0.5);
END IF;
INSERT INTO fatura VALUES(new.uid,temp);
END
delimiter ;

Facing Error while creating a MYSQL trigger

DELIMITER $$
DROP TRIGGER IF EXISTS TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE $$
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END$$
DELIMITER ;
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 '' at line 17
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
END IF;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END
$$

How to dissallow updates to a table if a column is a certain value in mysql?

I dont want anyone to be able to update thier Name to certain values and cancel the update if it is attempting to update the PLayername to "Error", and need to prevent it at the database level.
I figured I could simply do this with a trigger, so I tried making one here.
delimiter $$
create trigger errorcheck before insert on player_data
for each row
begin
if new.PlayerName = 'Error' then
signal sqlstate '45000';
end if;
end;$$
but the server responds with this 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 7
Thank you for any help / suggestions
delimiter $$
create trigger errorcheck before insert on player_data
for each row
begin
if new.PlayerName = 'Error' then
signal sqlstate '45000';
end if;
end;
I dont understand why but removing the $$ delimeter at the end saved the trigger and i tested it and it works how i wanted it to.

MYSQL Trigger syntax issue

I am trying this trigger but it keeps giving me this error:
Trigger
DELIMITER $$
CREATE
TRIGGER entries_limit_trigger
BEFORE INSERT ON Feed
FOR EACH ROW
BEGIN
Set #counts=(SELECT count(*) from Feed);
IF (#counts > 10000)
THEN
PRINT 'INSERTED SUCCESS';
END IF;
END
$$
DELIMITER ;
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 ''INSERTED SUCCESS'; END IF; END' at line 9
If you want the trigger to exit with a message you can do:
CREATE
TRIGGER entries_limit_trigger
BEFORE INSERT ON Feed
FOR EACH ROW
BEGIN
Set #counts=(SELECT count(*) from Feed);
IF #counts > 10000 THEN
set #msg = "Reached the limit";
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = #msg;
END IF;
END;
sqlfiddle demo
To make it exit with a message, just add another insert to the fiddle.