Topic: 这样的触发器怎么建立呀?

  Print this page

1.这样的触发器怎么建立呀? Copy to clipboard
Posted by: mengxiangjava
Posted on: 2008-10-27 10:15

我有个表t_user此表的id是自动增加的,怎样才能把它的id插入其他两个表t_habit(user_id) ,t_user_info(user_id)用触发器实现,也就是说,但插入t_user表信息时,其它两个表会自动把t_user表的id插入其中,怎么实现?急! 急!急!急!急!急!急!急!急!

2.Re:这样的触发器怎么建立呀? [Re: mengxiangjava] Copy to clipboard
Posted by: mengxiangjava
Posted on: 2008-10-27 10:38

CREATE DEFINER='95try'@`%` TRIGGER `user_info_trigger` AFTER INSERT ON `t_user`
FOR EACH ROW insert into t_user_info (user_id)
select b.id from t_user b
where b.id = LAST_INSERT_ID();

3.Re:这样的触发器怎么建立呀? [Re: mengxiangjava] Copy to clipboard
Posted by: mengxiangjava
Posted on: 2008-10-27 11:14

DROP TRIGGER IF EXISTS `user_info_trigger`;
CREATE DEFINER='95try'@`%` TRIGGER `user_info_trigger` AFTER INSERT ON `t_user`
FOR EACH ROW begin
insert into t_user_info (user_id)
select b.id from t_user b
where b.id = LAST_INSERT_ID();

insert into t_habit (user_id)
select b.id from t_user b
where b.id = LAST_INSERT_ID();

insert into t_corp (user_id)
select b.id from t_user b
where b.id = LAST_INSERT_ID();

end;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923