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 firstbeginandendkeywords are the culprits. Just enclose your originalcreateblock with aDELIMITER, as follows. I use#as the delimiter in my example, and I recognize that you use$$, though there will be no resulting difference.Also, there will be no difference if you end it like I did (
end#), or like you did, with the semicolon after theendkeyword: