mysql weird trigger errors - mysql

I'm trying to set up a trigger (run from a separate .sql file) to prevent insertion like this:
create trigger insert_formulas before insert on formulas
for each row
if ((new.formula_id_1 is not null) and (new.semantic_id_1 is not null))
or ((new.formula_id_2 is not null) and (new.semantic_id_2 is not null))
then
signal sqlstate '45100';
set #message_text = 'A formula cannot be a definition at the same time';
end if;
It produces an error 1064 near '' at line 6 and near 'end if' at line 1. What have I done wrong?
Thanks in advance.

Does the 9.4. User-Defined Variables #message_text is different from MESSAGE_TEXT from the definition of 13.6.7.5. SIGNAL Syntax?
The following code creates the trigger correctly:
DELIMITER $$
CREATE TRIGGER `insert_formulas` BEFORE INSERT ON `formulas`
FOR EACH ROW
BEGIN
IF((NEW.`formula_id_1` IS NOT NULL AND NEW.`semantic_id_1` IS NOT NULL) OR
(NEW.`formula_id_2` IS NOT NULL AND NEW.`semantic_id_2` IS NOT NULL)) THEN
SIGNAL SQLSTATE '45100'
SET MESSAGE_TEXT = 'A formula cannot be a definition at the same time';
END IF;
END$$
DELIMITER ;
Here SQL Fiddle.

Related

MySQL error code #1064 near IF ELSE block when creating a Trigger Before Insert

I am trying to put a constraint on a database where if generic_asset.type = 'raw' then generic_asset.atomic = 1 must be maintained. For this I wrote the following trigger of type BEFORE INSERT. Here is the snippet:
DELIMITER //
CREATE TRIGGER generic_asset_check BEFORE INSERT ON generic_asset FOR EACH ROW
BEGIN
IF NEW.type = 'raw' THEN
BEGIN
IF NEW.atomic = 0 THEN
SET SQLSTATE = 'Sorry cannot insert';
END IF;
END IF;
END //
DELIMITER ;
Error is like:
#1064 - syntax error near 'SQLSTATE = 'Sorry cannot insert';
END IF;
END IF;
END' in line 6
(translated from French).
I tried various syntax but all seam not to work and also knowing that my changes are so little like changing double quotes, removing BEGIN with END IF;... So I know these turns are irreverent.
I revised syntax in many internet resources and official documentation, nothing helped.
MySQL's IF statement does not take a BEGIN keyword. Also, if you want to raise an error from within the trigger, you need SIGNAL. Finally, these two nested conditions can be flattened.
Consider:
DELIMITER //
CREATE TRIGGER generic_asset_check BEFORE INSERT ON generic_asset FOR EACH ROW
BEGIN
IF NEW.type = 'raw' AND NEW.atomic = 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Sorry cannot insert';
END IF;
END //
DELIMITER ;
Demo on DB Fiddle

Trigger errors mysql

I want to make a trigger that prevent inserting overlaping dates. For example:
If I have an offer "Oferta" with the date from 1/3/2016 to 5/3/2016, I can't insert a new offer with the date 2/3/2016 to 4/3/2016 or 4/3/2016 to 7/3/2016
My SELECT checks that condition I believe. What I don't know how to do is to make an error if such happens. I'm new to triggers and Im having syntax errors, I checked triggers syntax but couldnt find the problem...
DELIMITER $$
CREATE TRIGGER tri_check_date_overlap BEFORE INSERT ON Oferta
FOR EACH ROW
BEGIN
IF EXISTS(
SELECT * FROM Oferta WHERE
(new.morada = morada AND new.codigo = codigo
AND ((new.data_inicio BETWEEN data_inicio AND data_fim)
OR new.data_fim BETWEEN data_inicio AND data_fim)
)
)
/*CALL raise_application_error(3001, 'Not odd number!'); */
DECLARE msg varchar(255);
set msg = concat('Error: That right is not allowed!', cast(new.right as char));
signal sqlstate '45000' set message_text = msg;
END $$
DELIMITER ;
Your logic is ok, I think you just missed the 'then' condition after 'if' and 'end if' before the end of the trigger.

Error creating a trigger to skip lines on insert in MySQL

I am having an error and I can't make sense of it. Here is the code for my trigger:
CREATE TRIGGER before_insert_test
BEFORE INSERT ON player_totals FOR EACH ROW
BEGIN
IF NEW.Player = 'Player' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'basketball-reference duplicate header';
END IF;
END;
I am getting red errors marks in MySQL workbench source code editor for 3 lines. For the line that begins SIGNAL SQLSTATE ..., It says:
Syntax Error: missing 'semicolon'
For the line that reads END IF;, the error says:
Syntax error: END (end) is not valid input as this position.
For the line that reads END;, the error says:
Extraneous input found - expected end of statement
Just not sure how to fix these errors, from what I've seen this looks like the correct syntax...
Try this one, with $$ after END
DELIMITER $$
CREATE TRIGGER before_insert_test
BEFORE INSERT ON player_totals
FOR EACH ROW
BEGIN
IF NEW.Player = 'Player' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'basketball-reference duplicate header';
END IF;
END; $$
DELIMITER ;

Whats the error in the following MySQL trigger

CREATE TRIGGER test BEFORE INSERT ON MYTABLE
FOR EACH ROW
BEGIN
IF break < 0 THEN
SIGNAL sqlstate '45000'
set message_text = 'ERROR';
END IF;
END;
This throws a syntax error and I'm not able to find it.
You need to change the delimiter that MySQL is using as an end-of-line marker. You need to do this for CREATE TRIGGER and CREATE PROCEDURE because if you don't MySQL will treat the first semi-colon as the end of the command and finish processing it prematurely - hence the syntax error.
Do this:
DELIMITER $$
CREATE TRIGGER test BEFORE INSERT ON MYTABLE
FOR EACH ROW
BEGIN
IF break < 0 THEN
SIGNAL sqlstate '45000'
set message_text = 'ERROR';
END IF;
END$$
DELIMITER ;
Now, MySQL will continue processing your CREATE TRIGGER command until it reaches $$, and include the semi-colons as part of the trigger script. Note that I've set the delimiter back to semicolon when I'm finished.

Mysql Prevent insert with insert trigger condition not met

I am trying to setup a trigger so an insert does not occur if a condition is not met.
I thought the below was the way to do it but i am not sure
I am getting an error
/* SQL Error (1407): Bad SQLSTATE: '45000 ' */
Can anyone let me know why I am getting this error and the best way for me to prevent an insert if the condition is not met in mysql.
DELIMITER $$
SHOW WARNINGS$$
USE `warrington_central`$$
CREATE TRIGGER before_insert_image_comment_section_check
BEFORE INSERT ON image_comment FOR EACH ROW
BEGIN
DECLARE error_msg varchar(255);
IF New.section != (SELECT id from section where section = "image")
THEN SET error_msg = "Cannot insert a comment into this section as it is the wrong section type";
SIGNAL SQLSTATE '45000 'SET MESSAGE_TEXT = error_msg;
END IF;
END
$$
SHOW WARNINGS$$
SQLSTATE is required to be a a 5 character string unless previously declared using DECLARE ... CONDITION;
SIGNAL SQLSTATE '45000 'SET MESSAGE_TEXT = error_msg;
You're trying to set SQLSTATE to '45000 ' (note the space) which is 6 characters long. Fix the spacing and you should not see the message again (it is also reflected in your error message, but the space is a bit hard to see)