資料庫當中處觸發器的應用

2022-02-20 06:44:51 字數 2421 閱讀 7937

1.先建立乙個test 和乙個test2  兩個表

use [wzdb]

go/****** object:  table [dbo].[test]    script date: 05/13/2013 14:21:21 ******/

set ansi_nulls on

goset quoted_identifier on

goset ansi_padding on

gocreate table [dbo].[test](

[id] [int] identity(1,1) not null,

[content] [varchar](500) not null,

constraint [pk_test] primary key clustered

([id] asc

)with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [primary]

) on [primary]

goset ansi_padding off

gouse [wzdb]

go/****** object:  table [dbo].[test2]    script date: 05/13/2013 14:21:45 ******/

set ansi_nulls on

goset quoted_identifier on

goset ansi_padding on

gocreate table [dbo].[test2](

[id] [uniqueidentifier] not null,

[name] [varchar](50) not null,

constraint [pk_test2] primary key clustered

([id] asc

)with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [primary]

) on [primary]

goset ansi_padding off

goalter table [dbo].[test2] add  constraint [df_test2_id]  default (newid()) for [id]

go2.先看一下新增的觸發器。當給test2 新增一條資料 相對應的給test1 也新增一條

use [wzdb]

go/****** object:  trigger [dbo].[trig_test2]    script date: 05/13/2013 14:22:38 ******/

set ansi_nulls on

goset quoted_identifier on

go--*****給test2新增資料的時候也改test1新增====

alter trigger  [dbo].[trig_test2]

on [dbo].[test2]   for  insert

as declare  @name  varchar(50)

-- declare  @name2  varchar(50)

select  @name=name  from inserted

insert into [wzdb].[dbo].[test]

([content])

values

(@name)

print '開始輸出'

print @name

print '輸出完畢'

2.刪除資料的觸發器

use [wzdb]

go/****** object:  trigger [dbo].[trrig_del]    script date: 05/13/2013 14:23:08 ******/

set ansi_nulls on

goset quoted_identifier on

goalter trigger [dbo].[trrig_del]

on  [dbo].[test]  for delete

as declare  @name varchar(50)

select @name=content from  deleted

if  exists(select * from test2  where name=@name)

begin

delete  from   test2  where  name=@name

print 'ddd'

end3.更新觸發器

還沒有完全理解,後續更新

資料庫應用 Sqlserver觸發器

下面講在sql sever2000 sybase資料為里設定觸發器的指令碼例項 sql sever2000和sybase的儲存語句是相同的 題目要求 表1和表2的主鍵都是xjh 學籍號 當表1新增,刪除,或者修改資料時,表2自動更新,請用觸發器實現它們?答案 建議在資料庫管理中心直接執行ddl的sq...

資料庫觸發器

觸發器是一種特殊型別的儲存過程,它不同於我們前面介紹過的儲存過程。觸發器主要是通過事件進行觸發而被執行的,而儲存過程可以通過儲存過程名字而 被直接呼叫。當對某一表進行諸如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...