Mysql trigger

 

mysql> show create table account G;

 

CREATE TABLE `account` (
  `acc_id` bigint(20) NOT NULL DEFAULT '0',
  `acc_first_name` varchar(100) DEFAULT NULL,
  `acc_last_name` varchar(100) DEFAULT NULL,
  `acc_email` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`acc_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

mysql> show create table account_update_log G;

 

 

CREATE TABLE `account_update_log` (
  `id` bigint(20) NOT NULL,
  `oldfirstname` varchar(100) DEFAULT NULL,
  `oldlastname` varchar(100) DEFAULT NULL,
  `oldemail` varchar(100) DEFAULT NULL,
  `newfirstname` varchar(100) DEFAULT NULL,
  `newlastname` varchar(100) DEFAULT NULL,
  `newemail` varchar(100) DEFAULT NULL,
  KEY `IDX_AccountLog_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

mysql> show create trigger trig_account_update G;

 

*************************** 1. row ***************************
               Trigger: trig_account_update
              sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTI
TUTION
SQL Original Statement: CREATE DEFINER=`poc`@`localhost` trigger trig_account_up
date
after update on account
for each row
begin
      insert into account_update_log values(
      old.acc_id,
      old.acc_first_name, old.acc_last_name, old.acc_email,
      new.acc_first_name, new.acc_last_name, new.acc_email
      );
END;

 

可以看到和Oracle的trigger很相似,提供行级trigger。

 

试试吧

mysql> select count(*) from account_update_log;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.03 sec)

 

mysql> select acc_id, acc_first_name from account where acc_id<=3;
+--------+------------------------+
| acc_id | acc_first_name         |
+--------+------------------------+
|      1 | david1
|      2 | david2                 |
|      3 | david3                 |
+--------+------------------------+

 

mysql> update account  set acc_first_name=concat(acc_first_name, '--updated') wh
ere acc_id<=3;
Query OK, 3 rows affected (0.05 sec)

 

mysql> select acc_id, acc_first_name from account where acc_id<=3;
+--------+-----------------+
| acc_id | acc_first_name  |
+--------+-----------------+
|      1 | david1--updated |
|      2 | david2--updated |
|      3 | david3--updated |
+--------+-----------------+
3 rows in set (0.00 sec)

 

mysql> select id, oldfirstname, newfirstname from account_update_log;
+----+--------------+-----------------+
| id | oldfirstname | newfirstname    |
+----+--------------+-----------------+
|  1 | david1       | david1--updated |
|  2 | david2       | david2--updated |
|  3 | david3       | david3--updated |
+----+--------------+-----------------+
3 rows in set (0.00 sec)




 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

inthirties

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值