Cancel DELETE with TRIGGERs - mysql

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 ;

Related

create a trigger need to update table 2 on update of table1

Would like to create a trigger in mysql need to
update t2.orderstatus as 'inactive' on update of t2.inactivedate and
update t2.orderstatus as 'active' on update of t2.activedate. Tried to adapt below one. but failed
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE TRIGGER trigger_on_stockhistory_update
AFTER update ON stockhistory.inactivedate
FOR EACH ROW
BEGIN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END;
$$
DELIMITER;
You can not set trigger on field change. It may be only on table level:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
IF OLD.inactivedate <> NEW.inactivedate THEN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END IF;
IF OLD.activedate <> NEW.activedate THEN
UPDATE mastersku
SET ordernow ='Active'
WHERE `SSKU` = NEW.SSKU;
END IF;
END$$
DELIMITER ;
Or a little elegant query using CASE statement:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
UPDATE mastersku
SET ordernow = CASE
WHEN OLD.inactivedate <> NEW.inactivedate THEN 'InActive'
WHEN OLD.activedate <> NEW.activedate THEN 'Active'
ELSE ordernow
END
WHERE
SSKU = NEW.SSKU;
END$$
DELIMITER ;

MySQL Trigger Is Not Inserting Date/Time String Into Column

Here's my trigger. Updating a row in event does not set a date/time string in sf_timestamp. New to triggers so I am not sure how to debug.
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END;;
DELIMITER $$
Suggestions?
When you define a DELIMITER, you need to keep that same delimiter until you are done with the block you are trying to declare. In this case, you are trying to create a new trigger and you need MySQL to interpret the whole block with multiple lines as one and ending them with the default ;
But when you are done, you have to END the block with the DELIMITER you have set earlier as following:
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;
Unless your update statement includes a value for sf_timestamp, that field likely doesn't have a value to be formatted. If you are attempting to update the timestamp to represent the time when the record is updated, why not just use the current time?
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp` BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = now();
END;;
DELIMITER $$
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;
Followed #pensums suggestion and this worked for me

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 ;

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.

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.

Categories