Creating trigger to prevent insert with condition from other table - mysql

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 ;

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.

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 trigger not working?

I am trying to create a trigger to insert a new row conditionally based on an insert on another table...I can't seem to nail the syntax.
Here is what I have thus far:
DELIMETER $$
CREATE TRIGGER overPricedCar
AFTER INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
END IF;
END$$
DELIMETER ;
For some reason I keep getting an error, they syntax seems to be OK, I'm not sure where I may have gone wrong.
EDIT
After correcting the typo, the trigger 'works'.
I have added a comment to be output when the trigger happens.
I have tested it, and the output message gets printed to the screen but the trigger does not actually complete the inserts:
DELIMITER $$
CREATE TRIGGER overPricedCar
BEFORE INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "New Gold car!"; // this line throws it off
END IF;
END$$
DELIMITER ;
Where can I place the messages I want to be printed to the screen when this trigger runs?
Typo:
DELIMETER
^--- should be an I: DELIMITER

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.

Cancel DELETE with TRIGGERs

I want to cancel a TRIGGER of DELETE. The idea is BEFORE DELETE, set column hide = "Y" then CANCEL the TRIGGER event and NOT really DELETE the row.
It's possible?
You can cancel the delete, however you cannot change the values in a DELETE trigger.
DELIMITER $$
CREATE TRIGGER bd_t1_each BEFORE DELETE ON t1 FOR EACH ROW
BEGIN
//This will not work, because the `NEW` virtual table does not exist in a delete trigger.
SET NEW.hide = 'Y';
//Raising an error will work.
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'DELETE canceled';
END $$
DELIMITER ;
Instead you should make a stored procedure to hide your rows:
DELIMITER $$
CREATE PROCEDURE delete_t1_row (pID INTEGER)
BEGIN
UPDATE t1 SET hide = 'Y' WHERE id = pID;
END $$
DELIMITER ;