Create Trigger Mysql - mysql

After insert in the table Aluguel update field Status to busy.
Not work.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = busy
WHERE id_apart = apartamento_id_apart;
END$$
DELIMITER ;

you have to use new key word
DELIMITER $$ CREATE TRIGGER Tgr_Status_Update AFTER UPDATE
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
---------------------------------------insert trigger -----------------
DELIMITER $$ CREATE TRIGGER Tgr_Status_Insert AFTER INSERT
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;

To refer to a column from the table you're inserting into, you need to use NEW.column_name.
And if busy is a string, you need to put it in quotes.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = 'busy'
WHERE id_apart = NEW.apartamento_id_apart;
END$$
DELIMITER ;
DEMO

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 ;

trigger two according to data

I have three tables and I have one trigger from table D_ISI in table 2 to trigger MAX_ISI value to write to the other table. Can you help me solve the trigger problem?
DELIMITER $$
CREATE TRIGGER `ISI_YUKSEK_ISE` BEFORE INSERT ON `TB1` FOR EACH ROW BEGIN
DECLARE
D_ISI INT ;
SET
D_ISI = NEW.D_ISI ; IF
(SELECT D_ISI = NEW.D_ISI FROM TB1,TB2 WHERE TB2.CIHAZ_ID=TB1.CIHAZ_KODU AND TB1.D_ISI >= TB2.MAX_ISI OR TB1.D_ISI <= TB2.MIN_ISI) THEN
INSERT
INTO
TB3(CIHAZ_KODU,D_ISI)
VALUES(NEW.CIHAZ_KODU,NEW.D_ISI) ;
END IF ; END
$$
DELIMITER ;

MySQL Insert Trigger not working with If Then Conditional

I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;

After insert, delete on other table trigger

I want a trigger that deletes the row in user_briefcases after a row is inserted in user_briefcases_sold
both tables have briefcase_id and user_id
I have no idea why this wont work:
CREATE TRIGGER delete_user_briefcase_when_sold
DELIMITER $$
AFTER INSERT ON user_briefcases_sold FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id && user_id = NEW.user_id;
END;
$$ DELIMITER ;
You have syntax error
try this
DELIMITER $$
CREATE
TRIGGER delete_user_briefcase_when_sold AFTER INSERT
ON user_briefcases_sold
FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id AND user_id = NEW.user_id;
END$$
DELIMITER ;

Result consisted of more than one row after activating trigger

procedure:
DELIMITER //
CREATE PROCEDURE sample_proc(IN wr VARCHAR(255))
BEGIN
SELECT some_function_with_result_code_int();
END;//
DELIMITER ;
and a trigger:
DELIMITER //
CREATE TRIGGER sample_trigger
AFTER UPDATE ON test
FOR EACH ROW
BEGIN
DECLARE some_name VARCHAR(255);
IF OLD.age <> NEW.age THEN
SELECT name INTO some_name FROM test WHERE OLD.age <> NEW.age;
CALL sample_proc(some_name);
END IF;
END;//
DELIMITER ;
Can't understand how updating only one row's column "age" could result in multiple row change...Any help will be appreciated.