Chinaunix首页 | 论坛 | 博客
  • 博客访问: 433975
  • 博文数量: 158
  • 博客积分: 1855
  • 博客等级: 上尉
  • 技术积分: 1888
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-18 14:19
文章分类

全部博文(158)

文章存档

2013年(4)

2012年(16)

2011年(10)

2010年(40)

2009年(61)

2008年(33)

分类: Mysql/postgreSQL

2009-05-14 17:49:08

[root@dlz ~]# mysql -h192.168.1.5 -uroot -paptech
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.0.56 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database aptech;
Query OK, 1 row affected (0.00 sec)
mysql> use aptech;
Database changed
eg1:
mysql> create table data(name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> create table chars(count int(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert chars
    -> values(0);
mysql> delimiter $$
mysql> create trigger aft_ins
    -> after insert on data for each row begin
    -> update chars
    -> set count=count+char_length(new.name);
    -> end;
    -> $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> insert into data
    -> values('benet');
Query OK, 1 row affected (0.01 sec)
mysql> select * from chars;
+-------+
| count |
+-------+
|     5 |
+-------+
1 row in set (0.00 sec)
eg2:
mysql> create table test(id int(4),qty float,price float);
Query OK, 0 rows affected (0.01 sec)
mysql> create table test_num(id int(4),num float);
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter $$
mysql> create trigger t
    -> after insert on test for each row begin
    -> update test_num
    -> set id=new.id,
    -> num=num+new.qty*new.price;
    -> end;
    -> $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> insert test_num
    -> values(1001,0);
Query OK, 1 row affected (0.00 sec)
mysql> insert test
    -> values(1005,3,2.5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test_num;
+------+------+
| id   | num  |
+------+------+
| 1005 |  7.5 |
+------+------+
1 row in set (0.00 sec)
查看trigger
mysql> show triggers \G;
*************************** 1. row ***************************
  Trigger: aft_ins
    Event: INSERT
    Table: data
Statement: begin
update chars
set count=count+char_length(new.name);
end
   Timing: AFTER
  Created: NULL
 sql_mode:
  Definer:
*************************** 2. row ***************************
  Trigger: t
    Event: INSERT
    Table: test
Statement: begin
update test_num
set id=new.id,
num=num+new.qty*new.price;
end
   Timing: AFTER
  Created: NULL
 sql_mode:
  Definer:
2 rows in set (0.00 sec)
ERROR:
No query specified
mysql>
删除触发器
mysql> drop trigger t;
Query OK, 0 rows affected (0.00 sec)
阅读(1037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~