MYSQL IF do nothing ELSE - mysql

I have a table users. When someone one to a insert new record which contains in name or in surname character diffrent from a-z,A-Z or space it will be a mistake and error occur. The code fails because after first THEN should be something, but i dont know what to put insidee. Because I only want to execute insert when if statement is true.
delimiter //
CREATE TRIGGER check_name_surname
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
IF NEW.name REGEXP '^[a-zA-Z' '.]+$' and NEW.surname REGEXP '^[a-zA-Z' '.]+$' then
ELSE
SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Incorrect name and surname';
END IF;
END;//
delimiter;

When you have nothing to put in your IF statement then you have to 'negate' the else condition and put it in the if.
So here you will throw an error IF name is not alphanumeric OR surname is not alphanumeric.
IF NEW.name NOT REGEXP '^[a-zA-Z' '.]+$' OR NEW.surname NOT REGEXP '^[a-zA-Z' '.]+$' THEN
SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Incorrect name and surname';
END IF;
Do not forget that the AND operator become OR.

Related

SQL Trigger chaining IF statements

I seem to be unable to have multiple, non-related if statements in SQL
I have a table of users with a bunch of columns that should be unique, but some are also optional. Since SQL throws an error when more than one fields have a NULL value, I decided to approach the problem with a trigger.
Here is how I wrote it:
create definer = root#localhost trigger `check-if-unique-update`
before UPDATE
on contact
for each row
if new.phone2 is null
then
if (select count(id) from contact where phone2 = new.phone2 or phone = new.phone2) > 0
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There is something wrong with the Contact data you entered!';
end if;
end if;
if new.email2 is not null
then
if (select count(id) from contact where email2 = new.email2 or email = new.email2)
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There is something wrong with the Contact data you entered!';
end if;
end if;
if new.fax is not null
then
end if;
end if;
First off, DataGrip (DataBase management IDE by JetBrains) gives me an error. When I then upload the file to the MySql DataBase it only shows the first if Statement
if new.phone2 is null
then
if (select count(id) from contact where phone2 = new.phone2 or phone = new.phone2) > 0
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There is something wrong with the Contact data you entered!';
end if;
end if;
I feel like I am missing some rudimentary detail, or is this just not possible in sql?
Have you tried including a begin-end in your trigger?
create definer = root#localhost trigger `check-if-unique-update`
before UPDATE
on contact
for each row
begin
if new.phone2 is null
then
if (select count(id) from contact where phone2 = new.phone2 or phone = new.phone2) > 0
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There is something wrong with the Contact data you entered!';
end if;
end if;
if new.email2 is not null
then
if (select count(id) from contact where email2 = new.email2 or email = new.email2)
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There is something wrong with the Contact data you entered!';
end if;
end if;
if new.fax is not null
then
end if;
end if;
end
Depending on your IDE, you may need to change the delimiter to // for example.
For reference see: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

IF ELSE statement is producing invalid syntax in MySQL

Not sure why this is returning invalid syntax:
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddColor`(
IN hexvalue varchar(7),
IN notes varchar(50))
BEGIN
IF hexvalue REGEXP '^#[0-9A-F]{6}$' THEN
INSERT INTO data.colors(hexvalue,notes) VALUES (hexvalue,notes);
ELSE
SIGNAL SQLSTATE '400'
SET MESSAGE_TEXT = 'Invalid hex value specified';
END IF;
END
There doesn't seem to be any errors when writing it in.
Based on the following official documentation:
The condition_value in a SIGNAL statement indicates the error value to be returned. It can be an SQLSTATE value (a 5-character string literal) or a condition_name that refers to a named condition previously defined with DECLARE ... CONDITION (see Section 13.6.7.1, “DECLARE ... CONDITION Syntax”).
Try the following syntax and change SIGNAL SQLSTATE value to 04000:
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddColor`(
IN hexvalue varchar(7),
IN notes varchar(50))
BEGIN
IF (hexvalue REGEXP '^#[0-9A-F]{6}$') THEN
INSERT INTO data.colors(`hexvalue`,`notes`) VALUES (hexvalue,notes);
ELSE
SIGNAL SQLSTATE '04000' SET MESSAGE_TEXT = 'Invalid hex value specified';
END IF;
END;

How can i check for a format of user input in sql query with a given regex format?

For example
Make a table such that user can enter either one of the two identity proofs:
Aadhaar card or Passport.
Formats:
Aadhaar Card: NNNN NNNN NNNN
Passport: ANNNNNN , where A is Alphabet and N is Number.
If the user enters wrong format, the input should not be accepted.
It is a better practice to validate the user input somewhere else (either in a frontend application or preferably in a business logic layer), but not in your database.
Having said that, it's still possible to validate incoming data and even apply changes to it using triggers. For example:
DELIMITER $$
CREATE TRIGGER checkInputFormat
BEFORE INSERT ON yourTable FOR EACH ROW
BEGIN
IF (NEW.card IS NULL AND NEW.passport IS NULL) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'No identity provided';
END IF;
IF (NEW.card IS NOT NULL AND NEW.card NOT REGEXP '^(\d{4} ){2}\d{4}$') THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid card format';
END IF;
IF (NEW.passport IS NOT NULL AND NEW.passport NOT REGEXP '^[A-Za-z]\d{6}$') THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid passport format';
END IF;
END;
$$
DELIMITER ;
This trigger will execute for each row that you try to insert in yourTable and check for the format of card and passport columns using regular expressions. If the format is invalid, an error will be thrown and the row won't be inserted.

How to trigger a RegEx condition on either of two columns in MySQL?

I have two columns (attributes) in a MySQL table, which a unique RegEx condition is to be applied on either of them via trigger. It means either column1 OR column2's condition suffices for the trigger to return 0.
In other words, how can I put OR between these columns?
I came up with this solution, but I'm not sure there is a better solution:
CREATE DEFINER = CURRENT_USER TRIGGER `MyDatabase`.`TableName_BEFORE_INSERT`
BEFORE INSERT ON `TableName` FOR EACH ROW
BEGIN
IF (NEW.column1,column2 REGEXP '^[augc]+' ) = 0 THEN
ROLLBACK;
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Your Input can contain only the following letters 'AUGC'. Please correct your input format;
END IF;
END
DELIMITER;
There are a few problems with your solution, it doesn't even compile, because:
COMMIT and ROLLBACK are not allowed in triggers
This syntax is wrong: NEW.column1,column2 REGEXP ...
there is a syntax error at this place ...letters 'AUGC'. Please..., you need to use double aphostrophes ...letters ''AUGC''. Please...
Try this trigger instead:
CREATE TRIGGER `TableName_BEFORE_INSERT`
BEFORE INSERT ON `TableName` FOR EACH ROW
BEGIN
IF NEW.column1 REGEXP '^[augc]+' OR NEW.column2 REGEXP '^[augc]+' THEN
-- ROLLBACK;
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Your Input can contain only the following letters ''AUGC''. Please correct your input format';
END IF;
END
/
Demo: http://sqlfiddle.com/#!9/27f5a

MySQL Trigger "syntax error, unexpected END, expecting ';'"

I'm trying to create a trigger to validate an email address in a customer table, but am getting the error
syntax error, unexpected END, expecting ';'
My code is as follows:
CREATE TRIGGER insert_validation_email
BEFORE INSERT ON customer FOR EACH ROW
BEGIN
IF (NEW.CustomerEmail NOT REGEXP '^[^#]+#[^#]+\.[^#]{2,}$')
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: invalid email address'
END* IF
END
END* is where it is underlined with the error (the asterisk is not there)
There are other validation triggers which will be included, so the begin and end statement is needed
Fixed by changing delimiter and putting ; after if block and final END. Thanks Gordon Linoff and a_horse_with_no_name for helping
DELIMITER $$
CREATE TRIGGER before_insert_email
BEFORE INSERT ON customer FOR EACH ROW
BEGIN
IF (NEW.CustomerEmail NOT REGEXP '^[^#]+#[^#]+\.[^#]{2,}$')
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: invalid email address';
END IF;
END$$
DELIMITER ;