如何编写一个MySQL触发器以将行插入到另一个表中?

如何编写一个MySQL触发器以将行插入到另一个表中?,第1张

如何编写一个MySQL触发器以将行插入到另一个表中?
drop table if exists comments;create table comments(comment_id int unsigned not null auto_increment primary key,user_id int unsigned not null)engine=innodb;drop table if exists activities;create table activities(activity_id int unsigned not null auto_increment primary key,comment_id int unsigned not null,user_id int unsigned not null)engine=innodb;delimiter #create trigger comments_after_ins_trig after insert on commentsfor each rowbegin  insert into activities (comment_id, user_id) values (new.comment_id, new.user_id);end#delimiter ;insert into comments (user_id) values (1),(2);select * from comments;select * from activities;

编辑:

mysql> . d:foo.sqlDatabase changedQuery OK, 0 rows affected (0.10 sec)Query OK, 0 rows affected (0.30 sec)Query OK, 0 rows affected (0.11 sec)Query OK, 0 rows affected (0.35 sec)Query OK, 0 rows affected (0.07 sec)Query OK, 2 rows affected (0.03 sec)Records: 2  Duplicates: 0  Warnings: 0+------------+---------+| comment_id | user_id |+------------+---------+|          1 |       1 ||          2 |       2 |+------------+---------+2 rows in set (0.00 sec)+-------------+------------+---------+| activity_id | comment_id | user_id |+-------------+------------+---------+|1 |          1 |       1 ||2 |          2 |       2 |+-------------+------------+---------+2 rows in set (0.00 sec)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5011626.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存