MYSQL觸發器記錄使用者操作的命令

來源:互聯網
上載者:User

標籤:命令   mysql   觸發器   記錄操作   

假如有一張重要的表btb,需要幾個管理員來管理

管理員:[email protected]、[email protected]、[email protected]


要求給表btb建立觸發器:

trigger觸發器需求:

   1、當在btb表中做insert、update、delete操作時,在btb_trace表中記錄執行操作的使用者、時間、操作命令。

   1、當在btb表中插入新記錄時,atb_bak表中會將atb表中的所有資料記錄下來。保持同步備份。

   2、當在btb表中刪除資料前,在atb_del表中將atb刪除的資料記錄一份。

   3、當在btb表中更新資料時,在atb_update表中將atb資料更新前後的資料記錄一份。

修改版(最佳化)

說明:觸發器中用new和old來向觸發器操作的表中傳遞資料。new表示操作欄位的新值(用於insert和update觸發事件),old表示欄位的舊值(用於delete觸發事件)。

格式:new.欄位名      ;old.欄位名。

建立使用者:

grant  all   on  *.*   to   [email protected]‘%‘  identified  by   ‘ma1‘  with  grant   option;

grant  all   on  *.*   to   [email protected]‘%‘  identified  by   ‘ma2‘  with  grant   option;

grant  all   on  *.*   to   [email protected]‘%‘  identified  by   ‘ma3‘  with  grant   option;

flush  privileges;

select  user,host,password   from   mysql.user;

 

建立與atb表結構相同的5張表:

use  test

create  table   btb(id  int,name  varchar(50));

create table btb_trace(name varchar(50),time varchar(50),act  varchar(20),id  varchar(20),sname  varchar(50));

create  table  btb_bak(user  varchar(100),id  int,name   varchar(50),del_time  varchar(50));

create  table  btb_del(user  varchar(100),id  int,name   varchar(50),del_time  varchar(50));

create  table  btb_update(user  varchar(100),id  int,name   varchar(50),del_time  varchar(50));

insert into btb_trace (name,time,act,id,sname) values(user(),now(),‘insert‘,0,  ‘root‘);

select  *   from   btb_trace;

 

刪除舊觸發器:

show    triggers \G

drop   trigger    insert_btb;

drop   trigger    delete_btb;

drop   trigger    update_btb;

 

觸發器1(insert觸發事件):

\d  $$

create  trigger insert_btb after  insert on  btb

for  each row

begin

insert  into btb_trace (name,time,act,id,sname) values(user(),now(),‘insert‘,new.id,new.name);

insert  into    btb_bak  values(user(),new.id,new.name,now());

end

$$

\d  ;

 

觸發器2(delete觸發事件):

\d  $$

create  trigger delete_btb  before  delete on   btb

for  each row

begin

insert  into btb_trace (name,time,act,id,sname) values(user(),now(),‘delete‘,old.id,old.name);

insert  into btb_del values(user(),old.id,old.name,now());

end

$$

\d  ;

 

觸發器3(update觸發事件):

\d  $$

create  trigger update_btb after  update on  btb

for  each row

begin

insert  into btb_trace(name,time,act,id,sname) values

(user(),now(),‘update‘,concat(old.id,‘->‘,new.id),concat(old.name,‘->‘,new.name));

insert  into btb_update values(user(),new.id,new.name,now());

end

$$

\d  ;

 

 

登入並測試:mysql   -uma1  -pma1    -h192.168.50.10

use   test;

insert  into   btb values(1,‘tom‘);

insert  into   btb values(2,‘jack‘);

insert  into   btb values(3,‘lucy‘);

update  btb   set  id=10  where   id=1;

delete  from  btb   where  id=3;

select  *   from  btb_trace;

select  *   from  btb_bak;

select  *   from  btb_update;

select  *   from  btb_del;

 


本文出自 “網路技術天地” 部落格,請務必保留此出處http://1364952.blog.51cto.com/1354952/1954611

MYSQL觸發器記錄使用者操作的命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.