- CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name
- FOR EACH ROW BEGIN
- [Statement]
- END
trigger_name:触发器名称
trigger_time:出发时间【after:执行后,before:执行前】
trigger_event:出发事件【INSERT、UPDATE、DELETE】
tbl_name:针对某张表
Statement:表达式
例如:
当某张表,新增一条记录,则在另外一张表中录入其orderID号
- CREATE TRIGGER trigger_name AFTER INSERT ON table_name
- BEGIN
- INSERT INTO other_table_name SET keyName=NEW.orderId, keyValue=NEW.orderId;
- END
例如:
当某张表,更新某字段时,根据判断是否满足条件,来更新新增或更新另外一张表
- CREATE TRIGGER trigger_name AFTER UPDATE ON table_name
- BEGIN
- DECLARE counts INT(1);
- IF OLD.ordState = 0 && NEW.ordState = 1 THEN
- select count(*) into counts from other_table_name where keyName=OLD.orderId;
- IF counts = 0 THEN
- INSERT INTO listen_ticket SET keyName=NEW.orderId, keyValue=NEW.orderId;
- END IF;
- END IF;
- END
OLD:未更新的数据信息
into Variable:将查询的返回值赋予某个变量