Mysql trigger checking the value before inserting - mysql

I want to create a trigger, that checks a value before inserting it to the table, but I am getting an error in syntax I don't know where is a mistake.
My code:
CREATE TRIGGER test1
BEFORE INSERT
ON payment for EACH ROW
BEGIN
IF(NEW.sum > 0)
THEN INSERT INTO payment (sum) VALUE (NEW.sum);
endif;
end;
Error I get: "#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 6"
Edit: Fixing it to end if; didn't help

CREATE TRIGGER test1
BEFORE INSERT
ON payment
FOR EACH ROW
BEGIN
IF NEW.sum <= 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`sum` value must be above zero.';
END IF;
END;

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 ;

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: print a warning message when an inserted entry is greater than a value

I have create a table as below:
mysql> create table testa (a int, b int, c real);
Query OK, 0 rows affected (0.14 sec)
But when I want to implement a trigger like this, I face some syntax errors:
mysql> create trigger testa_trig
before insert ON testa
FOR EACH ROW
WHEN (NEW.c > 100)
BEGIN
Print "Warning: c > 100!"
END;
ERROR 1064 (42000): 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 'WHEN (NEW.c > 100)
BEGIN
Print "Warning: c > 100!"
END' at line 4
I have checked the documentation at http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html but can't figure out the problem!
My MySQL version:
Server version: 5.5.38-0ubuntu0.12.04.1 (Ubuntu)
Based on the comments below, I tried the following cases, but also crashed:
mysql> create trigger testa_trig before insert on testa for each row
if (NEW.c > 100) begin insert into testb set bc=NEW.c end;
ERROR 1064 (42000): 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 'begin insert into testb set bc=NEW.c end' at line 1
Couple of things wrong here.
Delimiters. When you make a MySQL procedure or trigger, you need to be very explicit about delimiters so the query interpreter can distinguish between ends of lines in your procedure and the end of your declaration.
Location of the BEGIN statement. It should be directly after FOR EACH ROW.
Use of WHEN instead of IF.
Use of PRINT instead to SIGNAL SQLSTATE '...' SET MESSAGE_TEXT = '...'. This is how you raise exceptions in MySQL 5.5+.
Here is code that should work!
DELIMITER $$
CREATE TRIGGER testa_trig
BEFORE INSERT ON testa
FOR EACH ROW BEGIN
IF (NEW.c > 100) THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning: c > 100!';
END IF;
END$$
DELIMITER ;
Also, you can display any message using the select command.
IF (NEW.c > 100) THEN
SELECT "Warning: c > 100!" AS Output;
END IF
Place above code inside the trigger. It will print the output

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.

Syntax errors in mysql with procedure, triggers and signal

I feel embarrassed that I have to come and ask for help with this, but as surely many before me have learned, it seems mySQL syntax error messages are about as useful as a pope hat on a grizzly bear. Attached is my first attempt at writing a trigger for a car company database. The table, can_lease, relates the id of an employee and the id of a car model. The trigger is expected to enforce two rules: 1) there can be at most 10 car models associated with 1 employee, and 2) the employee must be of type leasing (there is a column 'leasing' which must equal 'Y').
So the goal is for the trigger to catch violations of this rule and send a signal and a message explaining the violation. I'm simply not sure what the errors are, but I will attach the relevant error messages as well.
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end;
delimiter $$
create trigger can_lease_insert_trigger
after insert on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
create trigger can_lease_update_trigger
after update on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
And here are the error messages:
ERROR 1064 (42000): 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
ERROR 1064 (42000): 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(rent_model_id) from can_lease where emp_id = eid) >= 10)
then' at line 1
ERROR 1064 (42000): 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 1
Thank you for your help! I would also appreciate any advice one has for debugging this sort of thing in general. Coming from gcc telling me at least something about why my code is wrong, this is a very foreign process!
EDIT: I realize that I should have moved the delimiter change up to above the procedure as well. I don't get it, but that removes all but one of the errors. Currently, the error is
ERROR 1064 (42000): 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(rent_model_id) from can_lease where emp_id = eid) == 10
then s' at line 4
The semicolons (;) between the first begin and end keywords are the culprits. Just enclose your original create block with a DELIMITER, as follows. I use # as the delimiter in my example, and I recognize that you use $$, though there will be no resulting difference.
DELIMITER #
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end#
Also, there will be no difference if you end it like I did (end#), or like you did, with the semicolon after the end keyword:
end;
#