mysql> delimiter // mysql> create trigger t_customer_insert before insert on customer -> for each row -> begin -> set NEW.customer_ssn = aes_encrypt(NEW.customer_ssn,'password'); -> end; -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> insert into customer values (1,'fred','smith','456097234'); Query OK, 1 row affected (0.00 sec) mysql> select * from customer;结果显示如下图:
mysql> delimiter // mysql> CREATE PROCEDURE cursor_demo() -> BEGIN -> DECLARE a, b CHAR(16); -> DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; -> OPEN cur1; -> REPEAT -> FETCH cur1 INTO a,b; -> UNDONE END REPEAT; -> CLOSE cur1; -> END -> //下面再举一个触发器的例子,该例子可以计算所有插入某表的某个列中的数值的和:
mysql> CREATE TABLE aclearcase/" target="_blank" >ccount (acct_num INT, amount DECIMAL(10,2)); mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account -> FOR EACH ROW SET @sum = @sum + NEW.amount;可见触发器功能可以提高管理人员管理数据库的灵活性。
(51CTO.COM教程)