[mysql] 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

This question is related to mysql sql-update triggers

The answer is


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;

DELIMITER //

CREATE TRIGGER contacts_after_insert
AFTER INSERT
   ON contacts FOR EACH ROW

BEGIN

   DECLARE vUser varchar(50);

   -- Find username of person performing the INSERT into table
   SELECT USER() INTO vUser;

   -- Insert record into audit table
   INSERT INTO contacts_audit
   ( contact_id,
     deleted_date,
     deleted_by)
   VALUES
   ( NEW.contact_id,
     SYSDATE(),
     vUser );

END; //

DELIMITER ;

Maybe remove the semi-colon after set because now the where statement doesn't belong to the update statement. Also the idRequest could be a problem, better write BookingRequest.idRequest


Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to sql-update

Update some specific field of an entity in android Room How to perform update operations on columns of type JSONB in Postgres 9.4 MySQL - UPDATE multiple rows with different values in one query How to update multiple columns in single update statement in DB2 Update Multiple Rows in Entity Framework from a list of ids Update MySQL using HTML Form and PHP CodeIgniter query: How to move a column value to another column in the same row and save the current time in the original column? mysql after insert trigger which updates another table's column Update values from one column in same table to another in SQL Server I want to use CASE statement to update some records in sql server 2005

Examples related to triggers

How to get JQuery.trigger('click'); to initiate a mouse click insert/delete/update trigger in SQL server How do you change Background for a Button MouseOver in WPF? mysql after insert trigger which updates another table's column creating triggers for After Insert, After Update and After Delete in SQL Why is the GETDATE() an invalid identifier Declare variable MySQL trigger How to emulate a BEFORE INSERT trigger in T-SQL / SQL Server for super/subtype (Inheritance) entities? MySql Error: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger jQuery click anywhere in the page except on 1 div