三個資料庫的觸發器

2021-06-20 18:52:50 字數 2842 閱讀 9201

當使用者a,b存放資料到tablec的時候,會將使用者a插入的資料的主鍵值和使用者名稱一起存放到tabled去

即tablec(id(pk),name,pass)a使用者插入(23,『a1』,『a2』)b使用者插入(20,『b1』,『b2』)

觸發器的作用是將(23,a)插入到表tabled去

mysql:

delimiter $$  --宣告結束符號為「$$」

create  trigger insert_user_t_trigger  before insert on  user_t 

for each row

begin

declare keyfieldname varchar(50) ;   

declare login_name varchar(50);

select column_name into  keyfieldname  from information_schema.key_column_usage 

where table_name='user_t';

select user() into login_name ;

if  (keyfieldname ='id' and  login_name= 'root@localhost' ) then

insert into u_t(key_id,key_name) values(new.id,login_name);

elseif ( keyfieldname ='username' and  login_name= 'root@localhost' ) then

insert into u_t(key_id,key_name) values(new.username,login_name);

else

insert into u_t(key_id,key_name) values(new.password,login_name);

end if;

end $$

oracle:

create or replace trigger insert_user_t_trigger  

after insert on user_t

for each row

declare

v_sql   varchar2(1000) ;

keyfieldname varchar2(50) ;

login_name varchar2(50) ;

begin

select c.column_name into keyfieldname from user_cons_columns c where c.table_name ='user_t' and c.position is not null and rownum = 1;

select user into login_name  from dual where rownum=1;

if inserting and keyfieldname ='id' and  login_name= 'scott' then

insert into u_t(key_id,key_name) values(:new.id,login_name);

elsif inserting and keyfieldname ='username' and  login_name= 'scott' then

insert into u_t(key_id,key_name) values(:new.username,login_name);

elsif inserting and keyfieldname ='password' and  login_name= 'scott' then

insert into u_t(key_id,key_name) values(:new.password,login_name);

end if;

end insert_user_t_trigger;

sqlserver:

create  trigger insert_user_t_trigger  on user_t 

for insert

asdeclare @keyfieldname varchar(50) ,@login_name varchar(50),@id varchar(50),@username varchar(50),@password  varchar(50);

select @keyfieldname=column_name   from information_schema.key_column_usage 

where table_name='user_t';

select @login_name= suser_name() ;

if  (@keyfieldname ='id' and  @login_name= 'sa' )

begin

select @id=id from inserted;

insert into u_t(key_id,key_name) values(@id,@login_name);

endelse  if( @keyfieldname ='username' and  @login_name= 'sa' )

begin

select @username=username from inserted;

insert into u_t(key_id,key_name) values(@username,@login_name);

endelse

begin

select @password=password from inserted;

insert into u_t(key_id,key_name) values(@password,@login_name);

end;

資料庫觸發器

觸發器是一種特殊型別的儲存過程,它不同於我們前面介紹過的儲存過程。觸發器主要是通過事件進行觸發而被執行的,而儲存過程可以通過儲存過程名字而 被直接呼叫。當對某一表進行諸如update insert delete 這些操作時,sql server 就會自動執行觸發器所定義的sql 語句,從而確保對資料...

資料庫觸發器

最近做了個觸發器的例子 create trigger tru user on user for update asif update status begin update user set stopflag 1 from inserted where user.userid inserted.us...

資料庫 觸發器

觸發器的概念 是使用者定義在關係表上的一類有事件驅動的特殊過程。一旦定義,任何對錶的增刪改操作均有伺服器自動啟用相應的觸發器,在dbms核心層進行集中的完整性控制。類似於約束,但比約束更靈活。觸發器的分類 dml觸發器 dml data manipulation language 觸發器是當資料庫伺...