MYSQL入門學習之十一:觸發器的基本操作
相關鏈接:
MYSQL入門學習之一:基本操作
http://www.BkJia.com/database/201212/173868.html
MYSQL入門學習之二:使用正則表達式搜索
http://www.BkJia.com/database/201212/173869.html
MYSQL入門學習之三:全文本搜索
http://www.BkJia.com/database/201212/173873.html
MYSQL入門學習之四:MYSQL的數據類型
http://www.BkJia.com/database/201212/175536.html
MYSQL入門學習之五:MYSQL的字符集
http://www.BkJia.com/database/201212/175541.html
MYSQL入門學習之六:MYSQL的運算符
http://www.BkJia.com/database/201212/175862.html
MYSQL入門學習之七:MYSQL常用函數
http://www.BkJia.com/database/201212/175864.html
MYSQL入門學習之八:數據庫及表的基本操作
http://www.BkJia.com/database/201212/175867.html
MYSQL入門學習之九:索引的簡單操作
http://www.BkJia.com/database/201212/176772.html
MYSQL入門學習之十:視圖的基本操作
http://www.BkJia.com/database/201212/176775.html
觸發器是MySQL響應以下任意語句而自動執行的一條MySQL語句(或位於BEGIN和END語句之間的一組語句): www.2cto.com
DELETE;
INSERT;
UPDATE;
使用觸發器,需要MySQL5或之後的版本支持。
一、觸發器基本操作
1、創建觸發器
創建觸發器時,需要給出4條信息:
唯一的觸發器名;(雖然MySQL5允許不同的表上的觸發器名稱相同,但一般最好不要這麼做。) www.2cto.com
觸發器關聯的表;
觸發器響應的事件;
觸發器何時執行;
語法結構:
create trigger trigger_name (BEFORE|AFTER) (delete|update|insert) on table_name
for each row
BEGIN
要觸發的sql語句;
END;
示例:
[sql]
mysql> delimiter |
mysql> create trigger t_trig before insert on t_goods for each row
-> begin
-> set NEW.add_date = current_date();
-> end;
-> |
mysql> delimiter ;
mysql> insert into t_goods(id,goods_name,quantity)
-> values(1,'apple',50);
-> |
mysql> select * from t_goods;
-> |
+------+------------+----------+------------+
| id | goods_name | quantity | add_date |
+------+------------+----------+------------+
| 1 | apple | 50 | 2012-12-12 |
+------+------------+----------+------------+
2、刪除觸發器
DROP TRIGGER [schema_name.]trigger_name;
示例:
mysql> DROP TRIGGER t_trig;
3、查看觸發器
SHOW TRIGGERS [ FROM DBNAME [ like '' ] ];
示例:
[sql] www.2cto.com
mysql> show triggers;
+---------+--------+---------+------------------------------------------------------------------------------------------
| Trigger | Event | Table | Statement
+---------+--------+---------+------------------------------------------------------------------------------------------
| t_trig | INSERT | t_goods | begin
set NEW.add_date = current_date();
end | BEFORE | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+---------+--------+---------+------------------------------------------------------------------------------------------
mysql> show triggers from test;
+---------+--------+---------+------------------------------------------------------------------------------------------
| Trigger | Event | Table | Statement
+---------+--------+---------+------------------------------------------------------------------------------------------
| t_trig | INSERT | t_goods | begin
set NEW.add_date = current_date();
end | BEFORE | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+---------+--------+---------+------------------------------------------------------------------------------------------
mysql> show triggers from test like 't%';
+---------+--------+---------+------------------------------------------------------------------------------------------
| Trigger | Event | Table | Statement
+---------+--------+---------+------------------------------------------------------------------------------------------
| t_trig | INSERT | t_goods | begin
set NEW.add_date = current_date();
end | BEFORE | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+---------+--------+---------+------------------------------------------------------------------------------------------
二、注意事項
1、只有表支持觸發器,視圖及臨時表都不支持;
2、每個表最多支持6個觸發器;
3、單一觸發器不能與多個事件或多個表關聯;
4、對於INSERT而言,新增的行用NEW來表示,行中的每一列的值,用NEW.列名來表示;
對於DELETE而言,刪除的行用OLD來表示;
對於UPDATE而言,更新前的行用OLD來表示,更新後的行用NEW來表示