MySQL: condition between two columns - mysql

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/

Related

unique constraint when column value is equal to false?

In mysql is it possible to have a unique constraint only when a value is false?
For example, I want a unique constraint on Name but only if Archived = false.
In other words I have a field archived which I'm using as a soft delete and deleting two things with the same name will result in a violation if I put the unique constraitn around Name and archived. hence wondering if the database can handle this type of unique constraint
Relevant Example
I think the only way to do this would be with Triggers, and Signals.
Just put your trigger for update and insert.
Relevant Example of Similar Approach From here: http://cvuorinen.net/2013/05/validating-data-with-triggers-in-mysql/
DELIMITER $$
CREATE TRIGGER example_before_insert_allow_only_one_active
BEFORE INSERT ON example_tbl FOR EACH ROW
BEGIN
IF NEW.active = 1 AND (SELECT COUNT(id) FROM example_tbl
WHERE active=1 AND foreign_key_id=NEW.foreign_key_id) > 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: only one active row allowed per type';
END IF;
END;
$$
CREATE TRIGGER example_before_update_allow_only_one_active
BEFORE UPDATE ON example_tbl FOR EACH ROW
BEGIN
IF NEW.active = 1 AND (SELECT COUNT(id) FROM example_tbl
WHERE id<>NEW.id AND active=1 AND foreign_key_id=NEW.foreign_key_id) > 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: only one active row allowed per type';
END IF;
END;
$$
Applied to your case
DELIMITER $$
CREATE TRIGGER example_before_insert_allow_only_one_not_archived
BEFORE INSERT ON example_tbl FOR EACH ROW
BEGIN
IF NEW.archived = 0 AND (SELECT COUNT(id) FROM example_tbl
WHERE name=NEW.name) > 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: existing name exists and you are entering with archived = 0 (false)';
END IF;
END;
$$
CREATE TRIGGER example_before_update_allow_only_one_not_archived
BEFORE UPDATE ON example_tbl FOR EACH ROW
BEGIN
IF NEW.archived = 0 AND (SELECT COUNT(id) FROM example_tbl
WHERE name=NEW.name) > 0
THEN
SIGNAL SQLSTATE '45000'
S SET MESSAGE_TEXT = 'Cannot add or update row: existing name exists and you are entering with archived = 0 (false)';
END IF;
END;
$$
Finally, you may actually be interested in using the following statement in your SQL if you only want one NAME for archived = false, but unlimited for archived = true.
SELECT COUNT(id) FROM example_tbl WHERE name=NEW.name AND archived = 0;

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.

Can MySQL TRIGGER check all INSERT values separetly?

I've got simple database with tables: Sklep(SklepNazwa,Adres), Towar(TowarNazwa,Producent), Sprzedaz(SklepNazwa,SklepAdres,TowarNazwa,TowarProducent,Cena,Data)
I had to create TRIGGER like shown below.
Problem is that it don't work when I try to INSERT several rows at once. When at least one row triggers SIGNAL all values are dumped. Nothing gets inserted, even when it is ok to be inserted.
Can I somehow call
INSERT INTO Sprzedaz
VALUES (...),(...),(...),(...);
where some values are ok to be inserted and some should fail and show SIGNAL message?
DELIMITER $$
CREATE TRIGGER SprzedazInsert
BEFORE INSERT ON `Sprzedaz`
FOR EACH ROW
BEGIN
IF (SELECT COUNT(1) FROM Sprzedaz
WHERE NEW.SklepNazwa = Sprzedaz.SklepNazwa
AND NEW.SklepAdres = Sprzedaz.SklepAdres
AND NEW.TowarNazwa = Sprzedaz.TowarNazwa
AND NEW.TowarProducent = Sprzedaz.TowarProducent
AND NEW.Cena = Sprzedaz.Cena
AND NEW.`Data` = Sprzedaz.`Data`) > 0
THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'INSERT FAILED: Row already exists.';
ELSE
IF (SELECT COUNT(1) FROM Sklep WHERE NEW.SklepNazwa = Sklep.SklepNazwa) > 0
THEN
IF (NEW.SklepAdres != (SELECT Adres FROM Sklep WHERE NEW.SklepNazwa = Sklep.SklepNazwa))
THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'INSERT FAILED: Wrong "Sklep" data.';
END IF;
ELSE
INSERT INTO `Sklep` (`SklepNazwa`,`Adres`)
VALUES (NEW.SklepNazwa,NEW.SklepAdres);
END IF;
IF (SELECT COUNT(1) FROM Towar WHERE NEW.TowarNazwa = Towar.TowarNazwa) > 0
THEN
IF (NEW.TowarProducent != (SELECT Producent FROM Towar WHERE NEW.TowarNazwa = Towar.TowarNazwa))
THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'INSERT FAILED: Wrong "Towar" data.';
END IF;
ELSE
INSERT INTO `Towar` (`TowarNazwa`,`Producent`)
VALUES (NEW.TowarNazwa,NEW.TowarProducent);
END IF;
END IF;
END$$
If you try to insert several rows in the same transaction, the dbms assumes you want all the inserts to succeed or fail as a group. If you want rows to succeed or fail individually, you need to commit after each insert.

Mysql triggers with multiple values

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 //

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 ;