Mysql triggers with multiple values - mysql

How can i do the same with username value in one trigger?
Thanks
DELIMITER //
CREATE TRIGGER notnullnameandsurname BEFORE INSERT ON guests FOR EACH ROW
BEGIN
IF new.name = "" then
SIGNAL SQLSTATE '45000';
END IF;
END //

How about using or?
DELIMITER //
CREATE TRIGGER notnullnameandsurname BEFORE INSERT ON guests FOR EACH ROW
BEGIN
IF new.name = '' or new.username = '' then
SIGNAL SQLSTATE '45000';
END IF;
END //

Related

MySQL "create trigger tr_emp before insert or update"

delimiter //
create trigger tr_emp2 before insert or update
on employee for each row
begin
if new.ID<>10100 then
if new.Mgr not in (select ID from employee)
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Invalid Mgr_id!';
end if;
end if;
end;//
delimiter ;
How to express "create trigger tr_emp before insert or update" ,is it a wrong exoression ?
You have to create two triggers, but you can move the common code into a procedure and have them both call the procedure.

Issue handling with MySQL trigger

This is my trigger for show error handling :
DELIMITER //
CREATE TRIGGER NOTA_PENJUALAN_INS BEFORE INSERT ON nota_penjualan FOR EACH ROW
BEGIN
DECLARE VDATE DATETIME;
If (NEW.INVENTORY_OUT_ID IS NOT NULL) then
CALL STATUS_INV_OUT(NEW.INVENTORY_OUT_ID);
SELECT (a.DOCUMENT_DATE) INTO VDATE
FROM inventory_out a
INNER JOIN NOTA_PENJUALAN b ON a.INVENTORY_OUT_ID=b.INVENTORY_OUT_ID
WHERE b.NOTA_PENJUALAN_ID=NEW.NOTA_PENJUALAN_ID;
End IF;
IF(VDATE > NEW.DOCUMENT_DATE) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'sorry can not exceed the date that has been specified';
END IF;
END
//
It displays:
And does not display my ShowMessage.

How to create MySQL trigger

I have table users with two fields id (numeric) and email (with full email adress) how to add trigger which can prevent adding new email records with specific domains eg: *#somedomain.com?
The trigger would look like this:
DELIMITER $$
CREATE TRIGGER USER_INSERT BEFORE INSERT ON USER
FOR EACH ROW
BEGIN
IF new.email like '%#somedomain.com' THEN
SIGNAL sqlstate '99999'
SET message_text = 'Invalid domain';
END IF;
END$$
DELIMITER ;
Please try this:
DELIMITER $$
CREATE TRIGGER example_before_insert_reject_domains
BEFORE INSERT ON example_tbl FOR EACH ROW
BEGIN
IF NEW.email LIKE '%#somedomain.com'
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: invalid domain for email';
END IF;
END;
$$

MySQL: condition between two columns

I have two columns:
commission (percental('yes','no'))
recurring(enum('yes','no').
I want to declare the following dependency:
recurring can only be 'yes', when percental is 'yes', too.
Is there a possibility to manage it within mysql?
you could create two triggers to check for this, sqlfiddle
CREATE
TRIGGER `before_insert` BEFORE INSERT
ON `commission`
FOR EACH ROW BEGIN
IF NEW.recurring = 'yes' AND NEW.percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
CREATE
TRIGGER `before_update` BEFORE UPDATE
ON `commission`
FOR EACH ROW BEGIN
IF NEW.recurring = 'yes' AND NEW.percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
since the code is the same for UPDATE and INSERT you might want to create a procedure to call for both triggers like this sqlfiddle
DROP PROCEDURE IF EXISTS check_commission_recurring_based_on_percental/
CREATE PROCEDURE check_commission_recurring_based_on_percental(IN percental ENUM('yes','no'), IN recurring ENUM('yes','no'))
BEGIN
IF recurring = 'yes' AND percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
CREATE
TRIGGER `before_insert` BEFORE INSERT
ON `commission`
FOR EACH ROW BEGIN
CALL check_commission_recurring_based_on_percental(NEW.percental,NEW.recurring);
END/
CREATE
TRIGGER `before_update` BEFORE UPDATE
ON `commission`
FOR EACH ROW BEGIN
CALL check_commission_recurring_based_on_percental(NEW.percental,NEW.recurring);
END/

Before insert trigger behavior in mysql

I created a table with some data in it, and a trigger like this:
DROP TRIGGER IF EXISTS verifica_cpf;
DELIMITER //
CREATE TRIGGER verifica_cpf
BEFORE INSERT ON `usuario` FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM usuario WHERE cpf = new.cpf) > 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'CPF EXISTENTE!';
END IF;
END//
DELIMITER ;
Basically it checks for duplicate 'cpf' values before inserts.
Let's say that in my table, there's one entry with this cpf value: 873.255.218-12
If i try to insert another entry with the same cpf value, the trigger won't work. But if i insert it again, it will.
How can i solve this?
You can change this by changing the 1 to 0:
DROP TRIGGER IF EXISTS verifica_cpf;
DELIMITER //
CREATE TRIGGER verifica_cpf
BEFORE INSERT ON `usuario` FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM usuario WHERE cpf = new.cpf) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'CPF EXISTENTE!';
END IF;
END//
DELIMITER ;
Or by using the more efficient if exists:
DROP TRIGGER IF EXISTS verifica_cpf;
DELIMITER //
CREATE TRIGGER verifica_cpf
BEFORE INSERT ON `usuario` FOR EACH ROW
BEGIN
IF exists (SELECT 1 FROM usuario WHERE cpf = new.cpf) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'CPF EXISTENTE!';
END IF;
END//
DELIMITER ;