创建触发器以更新同一表中的特定列后更新某些列

时间:2019-11-25 20:11:00

标签: sql mariadb

我有一个名为BrrowedBookBy的表 here is the table description

+--------------------+-------------+------+-----+---------+-------+
| Field              | Type        | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| ISBN_B             | char(50)    | YES  |     | NULL    |       |
| PIN_M              | int(11)     | YES  |     | NULL    |       |
| StartDate          | date        | YES  |     | NULL    |       |
| EndDate            | date        | YES  |     | NULL    |       |
| BrrowBookCondition | tinyint(1)  | YES  |     | NULL    |       |
| Late               | tinyint(1)  | YES  |     | NULL    |       |
| ReNew              | tinyint(1)  | YES  |     | NULL    |       |
| Fine               | int(11)     | YES  |     | NULL    |       |
| Fine_Description   | varchar(30) | YES  |     | NULL    |       |
+--------------------+-------------+------+-----+---------+-------+
9 rows in set (0.040 sec)

所以我想在更新BorrowBookCondition列时设置触发器

我尝试过这种方法,但是在Maria却没有工作

CREATE DEFINER=`root`@`localhost` TRIGGER Set_fine
  AFTER Update ON BorrowedBookBy
   FOR EACH ROW
    UPDATE Fine SET Fine = 50 and fine_description = “Not Meet The Condition”
      where BrrowBookCondition in (select * from BorrowedBookBy where BrrowBookCondition =0);

如果我在没有任何条件的情况下以这种方式键入它,就可以正常工作

CREATE DEFINER=`root`@`localhost` TRIGGER Set_fine
  AFTER Update ON BorrowedBookBy
   FOR EACH ROW
    UPDATE Fine SET Fine = 50 and fine_description = “Not Meet The Condition”;

1 个答案:

答案 0 :(得分:0)

我很困惑。您的触发器引用的是另一个名为Fine的表,但是您没有提到它。

确定要在要更新的同一行中设置它吗?

如果是这样:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` TRIGGER Set_fine BEFORE Update ON BorrowedBookBy
FOR EACH ROW
BEGIN
    IF NEW.BrrowBookCondition = 0 THEN
        SET Fine = 50;
        SET fine_description = 'Not Meet The Condition;
    END IF;
END;

DELIMITER ;