插入触发器后更新另一个表的列的mysql

时间:2022-06-28 00:06:12

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

我正在尝试编写触发器,我有以下表格: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:

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

我需要一个触发器,如果​​具有相同ID的请求被插入OccupiedRoom表,将在BookingReques中将状态更改为1,所以我尝试了这样的事情

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

它不起作用,所以任何建议都会非常有用

4 个解决方案

#1


26  

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 ;

#2


22  

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

根据您的要求,您不需要在触发器中使用BEGIN END和IF以及不必要的SELECT。所以你可以简化它

CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom 
FOR EACH ROW
  UPDATE BookingRequest
     SET status = 1
   WHERE idRequest = NEW.idRequest;

#3


1  

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

也许在set之后删除分号,因为现在where语句不属于update语句。 idRequest也可能是个问题,最好写一下BookingRequest.idRequest

#4


-2  

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 ;

#1


26  

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 ;

#2


22  

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

根据您的要求,您不需要在触发器中使用BEGIN END和IF以及不必要的SELECT。所以你可以简化它

CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom 
FOR EACH ROW
  UPDATE BookingRequest
     SET status = 1
   WHERE idRequest = NEW.idRequest;

#3


1  

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

也许在set之后删除分号,因为现在where语句不属于update语句。 idRequest也可能是个问题,最好写一下BookingRequest.idRequest

#4


-2  

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 ;