sql触发器

use TEST

go

if exists(select * from sysobjects where name='Student')

drop table Student

go

create table Student (

StudentID int primary key,

Name nvarchar(50) )

go

if exists(select *from sysobjects where name='BorrowRecord')

drop table BorrowRecord

go

create table BorrowRecord

( BorrowRecord int identity(1,1),

StudentID int, BorrowDate datetime,

ReturnDate datetime )

go

if exists(select *from sysobjects where name='truStudent')

drop trigger truStudent

go

create trigger truStudent

on Student

for update

as

if update(StudentID)

begin update BorrowRecord set StudentID=i.StudentID

from BorrowRecord br, deleted d,inserted i where br.StudentID=d.StudentID

end

if exists(select *from sysobjects where name='strStudent')

drop trigger strStudent

go

create trigger strStudent

on Student

for Delete

as

delete BorrowRecord from BorrowRecord br,deleted d where br.StudentID=d.StudentID

GO

delete from Student where StudentID=1005

posted @ 2016-07-06 14:37  恋之呓  阅读(135)  评论(0编辑  收藏  举报