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
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.
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.
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.
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 ;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With