Syntax errors in mysql with procedure, triggers and signal - mysql

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;
#

Related

Can I update just added row in MySQL with trigger based on condition?

I need to update the salary of a newly added employee in the same table if condition is satisfied. SQL keeps throwing errors about syntax, if I use delimiter change it looks like this:
delimiter //
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;//
delimiter ;
But it throws an 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
'delimiter //
create trigger fixSalary
after insert on emp
for each row
begin
if ' at line 1"
I tried removing the delimiter:
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;
which resulted in another 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 8
I've tried all kinds of semicolons combinations with no luck. Can anyone help me to understand what's going on?

Mysql trigger checking the value before inserting

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;

MYSQL syntax delimiter and syntax error

I have tried to fix the syntax errors in the following but I can't see what on earth is wrong here:
DELIMITER =
CREATE TRIGGER trigs BEFORE UPDATE ON autoinc
FOR EACH ROW BEGIN
DECLARE num_rows INTEGER;
SELECT (*) INTO num_rows FROM autoinc;
IF num_rows >=3 THEN
DELETE FROM autoinc LIMIT 1;
END IF;
END=
DELIMITER ;
The errors are:
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 '*) INTO num_rows FROM autoinc; IF num_rows >' at line 4
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 '3 THEN
Can any please help me fix this?
As already mentioned in the comments:
SELECT (*) AS num_rows ...
was probably meant to be
SELECT COUNT(*) AS num_rows ...
And the
IF num_rows >=3 THEN
breaks as you defined = as delimiter.
Use a delimiter that doesn't occur in your code, e.g.:
DELIMITER //
With these two changes things should work without syntax errors

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

SQL syntax error,

This small query is driving me crazy. I keep getting errors no matter what I try
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 6
This is the code
CREATE PROCEDURE deposit (
IN custAccount INT(11),
IN amount INT(11)
)
IF amount < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid amount';
END IF;
The procedure is incomplete, dont judge it yet :D
Thanks
"The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself."