Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql after insert trigger which updates another table's column

i'm trying to write a trigger, I have following tables: BookingRequest:

  +-----------+---------+------+-----+---------+----------------+     | Field     | Type    | Null | Key | Default | Extra          |     +-----------+---------+------+-----+---------+----------------+     | idRequest | int(11) | NO   | PRI | NULL    | auto_increment |     | roomClass | int(11) | NO   |     | NULL    |                |     | inDate    | date    | NO   |     | NULL    |                |     | outDate   | date    | NO   |     | NULL    |                |     | numOfBeds | int(11) | NO   |     | NULL    |                |     | status    | int(11) | NO   | MUL | NULL    |                |     | idUser    | int(11) | NO   | MUL | NULL    |                |     +-----------+---------+------+-----+---------+----------------+ 

status table:

+------------+--------------------------------------------------+------+-----+---------+-------+ | Field      | Type                                             | Null | Key | Default | Extra | +------------+--------------------------------------------------+------+-----+---------+-------+ | idStatus   | int(11)                                          | NO   | PRI | NULL    |       | | nameStatus | enum('underConsideration','approved','rejected') | YES  |     | NULL    |       | +------------+--------------------------------------------------+------+-----+---------+-------+ 

OccupiedRoom:

+--------------+---------+------+-----+---------+----------------+ | Field        | Type    | Null | Key | Default | Extra          | +--------------+---------+------+-----+---------+----------------+ | idOccupation | int(11) | NO   | PRI | NULL    | auto_increment | | idRoom       | int(11) | NO   |     | NULL    |                | | idRequest    | int(11) | NO   |     | NULL    |                | +--------------+---------+------+-----+---------+----------------+ 

i need a trigger which will change status in BookingReques to 1 if request with the same id is inserted into OccupiedRoom table, so i tried something like this

create trigger occupy_trig after insert on OccupiedRoom  for each row begin   if BookingRequest.idRequest= NEW.idRequest    then        update BookingRequest        set status = '1';        where idRequest = NEW.idRequest;      end if; END; 

and it doesn't work, so any suggestions would be very appriciated

like image 546
DeadKennedy Avatar asked Jun 03 '13 07:06

DeadKennedy


People also ask

What is after insert trigger?

The AFTER INSERT trigger syntax parameter can be explained as below: First, we will specify the name of the trigger that we want to create. It should be unique within the schema. Second, we will specify the trigger action time, which should be AFTER INSERT clause to invoke the trigger.

Does an on update trigger have access to old and new variables?

UPDATE. An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT. An INSERT trigger can only refer to a NEW transition variable because before the activation of the INSERT operation, the affected row does not exist in the database.

Can we use update in trigger?

AFTER UPDATE Trigger in SQL is a stored procedure on a database table that gets invoked or triggered automatically after an UPDATE operation gets successfully executed on the specified table. For uninitiated, the UPDATE statement is used to modify data in existing rows of a data table.


2 Answers

Try this:

DELIMITER $$ CREATE TRIGGER occupy_trig AFTER INSERT ON `OccupiedRoom` FOR EACH ROW begin        DECLARE id_exists Boolean;        -- Check BookingRequest table        SELECT 1        INTO @id_exists        FROM BookingRequest        WHERE BookingRequest.idRequest= NEW.idRequest;         IF @id_exists = 1        THEN            UPDATE BookingRequest            SET status = '1'            WHERE idRequest = NEW.idRequest;         END IF; END; $$ DELIMITER ; 
like image 147
user4035 Avatar answered Sep 18 '22 12:09

user4035


With your requirements you don't need BEGIN END and IF with unnecessary SELECT in your trigger. So you can simplify it to this

CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom  FOR EACH ROW   UPDATE BookingRequest      SET status = 1    WHERE idRequest = NEW.idRequest; 
like image 37
peterm Avatar answered Sep 22 '22 12:09

peterm