MySQL "create trigger tr_emp before insert or update" - mysql

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.

Related

Creating trigger to prevent insert with condition from other table

im trying to create trigger that prevent insert on buku_dalam_pinjam with certain condition from other table which is anggota_dosen. this is my current trigger
CREATE DEFINER=`root`#`localhost` TRIGGER `buku_dalam_pinjam_BI`
BEFORE INSERT ON `buku_dalam_pinjam`
FOR EACH ROW BEGIN
if (buku_dalam_pinjam.id_agt_dosen=anggota_dosen.id_agt_dosen and
anggota_dosen.ttl_proses_pinjam >=5) then
signal sqlstate '45000' set message_text="error message";
end if;
END
it was succesfully created, then when i try to insert data on buku_dalam_pinjam
it gave me error message, but when i erase the trigger it let me insert on buku_dalam_pinjam table. so is there any mistake in my trigger?
anggota_dosen table
buku_dalam_pinjam table
error message when i include the trigger
try this
drop trigger if exists buku_dalam_pinjam_BI;
delimiter $$
CREATE TRIGGER `buku_dalam_pinjam_BI`
BEFORE INSERT ON buku_dalam_pinjam
FOR EACH ROW BEGIN
if exists (select 1 from anggota_dosen
where new.id_agt_dosen = anggota_dosen.id_agt_dosen and
anggota_dosen.ttl_proses_pinjam >=5) then
signal sqlstate '45000' set message_text="error message";
end if;
END $$
delimiter ;

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;
$$

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 ;

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.

Prevent a record form being deleted or updated via the use of a Mysql trigger

I am using Mysql and trying to create a trigger on a table that prevents a specific record from being deleted or updated, example I have a table demo
id username password
1 dames t312llok
2 sauce 12ff1fff1
3 hynes 5656166oo9
I would like to prevent the record:
id username password
1 dames t312llok
from being deleted or updated via the use of a trigger
If you are using MySQL 5.5 or higher, this is simple.
DELIMITER $$
DROP TRIGGER IF EXISTS demo_bd $$
CREATE TRIGGER demo_bd BEFORE DELETE ON demo FOR EACH ROW
BEGIN
IF OLD.id = 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This magical user cannot be deleted';
END IF;
END $$
DELIMITER ;
For the update, it's exactly the same code, with minor tweaks.
DELIMITER $$
DROP TRIGGER IF EXISTS demo_bu $$
CREATE TRIGGER demo_bu BEFORE UPDATE ON demo FOR EACH ROW
BEGIN
IF OLD.id = 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This magical user cannot be updated';
END IF;
END $$
DELIMITER ;
Also... don't store passwords in the database.