日期:2014-05-17  浏览次数:20513 次

如何做两个表之间的触发器?
学生表:学号,姓名,年龄,系号,系名
系表:系号,系名,姓名,年龄。

如何在学生表中增加一条记录,系表中也随之自动增加。同理,删除怎么做?

------解决方案--------------------
当然可以!
你可以仿照这篇博文进行 http://www.cnblogs.com/nicholas_f/archive/2009/09/22/1572050.html

希望成功后能共享出你的成果
------解决方案--------------------
我没有做过,所以才让你去看人家的做的例子
要是让你看手册,不就太那个了吗
------解决方案--------------------
delimiter //
DROP TRIGGER IF EXISTS trigger_on_tab1//
CREATE TRIGGER trigger_on_tab1
AFTER INSERT ON test1 
FOR EACH ROW 
BEGIN
insert into test2(test1_id,test1_name) values(new.id, new.name); 
END//

一个列子,可以借鉴下哦!
------解决方案--------------------
语法错了。贴出你的SQL串看看。
------解决方案--------------------
语句没错,应该是分界符的问题。
执行该触发器之前先将分节符;修改下再执行创建
delimiter $
drop trigger if exists t_afterinsert_on_tab1$
create trigger t_afterinsert_on_tab1
after insert on TAB1
for each row
begin
insert into tab2(tab2_name) values (new.tab1_name);
end$
insert into tab1 (tab1_name) values ('张三')$
这段代码试下看
------解决方案--------------------
SQL code

以eschop的商品表,跟订单表为例:
新建商品表
create table goods(
    id int auto_increment primary key, #商品id
    name varchar(30) not null default '',#商品名
    num tinyint not null default 0    #商品数量
)engine  myisam default charset utf8;
新建订单变
create table indent(
    oid int auto_increment primary key,    #订单id
    gid int not null default 0,    #商品id
    much tinyint not null default 0    #购买数量
)engine myisam default charset utf8;
mysql> desc goods;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(30) | NO   |     |         |                |
| num   | tinyint(4)  | NO   |     | 0       |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set
mysql> desc indent;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| oid   | int(11)    | NO   | PRI | NULL    | auto_increment |
| gid   | int(11)    | NO   |     | 0       |                |
| much  | tinyint(4) | NO   |     | 0       |                |
+-------+------------+------+-----+---------+----------------+
插入演示数据:
insert into goods values(1,'三星手机',12),(2,'ipad电脑',19),(3,'摩托罗拉mp3',38);
mysql> select * from goods;
+----+-------------+-----+
| id | name        | num |
+----+-------------+-----+
|  1 | 三星手机    |  12 |
|  2 | ipad电脑    |  19 |
|  3 | 摩托罗拉mp3 |  38 |
+----+-------------+-----+
手工给订单表添加购买记录:
insert into indent(gid,much)values(3,2);
mysql> select * from indent;
+-----+-----+------+
| oid | gid | much |
+-----+-----+------+
|   1 |   3 |    2 |
+-----+-----+------+
1 row in set
手工给商品表减少商品信息:
update goods set num=num-2 where id=3;
mysql> select * from goods;
+----+-------------+-----+
| id | name        | num |
+----+-------------+-----+
|  1 | 三星手机    |  12 |
|  2 | ipad电脑    |  19 |
|  3 | 摩托罗拉mp3 |  36 |
+----+-------------+-----+
3 rows in set
修改mysql的结束符:
mysql> delimiter $
-------------------------------------------
创建触发器
create trigger tg1
after insert  #在插入之后触发
on indent
for each row  #固定写法
begin
update goods set num=num-1 where id=3;
end
$
------------------------------------------
模拟用户下订单流程
商品表:
+----+-------------+-----+
| id | name        | num |
+----+-------------+-----+
|  1 | 三星手机    |  12 |
|  2 | ipad电脑    |  19 |
|  3 | 摩托罗拉mp3 |  36 |
+----+-------------+-----+
订单表:
+-----+-----+------+
| oid | gid | much |