# 创建表 创建触发器
mysql>
CREATE
TABLE
account (acct_num
INT
, amount
DECIMAL
(10,2));
Query OK, 0
rows
affected (0.03 sec)
mysql>
INSERT
INTO
account
VALUES
(137,14.98),(141,1937.50),(97,-100.00);
mysql> delimiter //
mysql>
CREATE
TRIGGER
upd_check BEFORE
UPDATE
ON
account
FOR
EACH ROW
BEGIN
IF NEW.amount < 0
THEN
SET
NEW.amount = 0;
ELSEIF NEW.amount > 100
THEN
SET
NEW.amount = 100;
END
IF;
END
;//
mysql> delimiter ;
# 验证触发器作用
mysql>
select
*
from
account;
+
| acct_num | amount |
+
| 137 | 14.98 |
| 141 | 1937.50 |
| 97 | -100.00 |
+
3
rows
in
set
(0.00 sec)
mysql>
update
account
set
amount = 114.98
where
acct_num = 137;
Query OK, 1 row affected (0.01 sec)
Rows
matched: 1 Changed: 1 Warnings: 0
mysql>
select
*
from
account;
+
| acct_num | amount |
+
| 137 | 100.00 |
| 141 | 1937.50 |
| 97 | -100.00 |
+
3
rows
in
set
(0.00 sec)
# 查看触发器
mysql> show triggers;
# 删除触发器
mysql>
drop
trigger
if exists upd_check;
# 查看数据库实例中所有触发器
SELECT
a.TRIGGER_SCHEMA,
a.TRIGGER_NAME,
a.ACTION_TIMING,
a.EVENT_OBJECT_TABLE,
a.EVENT_MANIPULATION
FROM
information_schema.`TRIGGERS` a
WHERE
a.TRIGGER_SCHEMA
NOT
IN
(
'information_schema'
,
'performance_schema'
,
'mysql'
,
'sys'
);
delimiter // 设置MySQL执行结束标志,默认为;