--触发器:一种特殊的存储过程,通过对数据库表操作的动作,来触发,增删改
select *from student
select *from score
select *From fruit
drop trigger shanchu
--第一种触发器 当某个动作之后触发
alter trigger xiugai
on Fruit
after update --修改之后的动作
as
select *From inserted --inserted 表示变动的那一行属性
go
update Fruit set Numbers=20 where Ids='k001'
drop trigger shanchu
--第二种触发器 替换某个动作
alter trigger shanchu
on student
instead of delete --替换删除
as
--先删除成绩表关于同学的数据
delete from score where sno in (select sno from deleted)
--在删除student 里面的数据
delete from student where sno in (select sno from deleted)
go
delete from Student where sno=101
--事务 保证流程完整执行,如果一步失败就回到起点
--事务:保证流程完整执行,如果一步失败就回到起点。
begin tran t1--开始事务
delete from course
if @@ERROR>0
rollback tran t1
delete from student where Sno=105
if @@ERROR>0
rollback tran t1
select *from student
if @@ERROR>0
rollback tran t1
select *from course
if @@ERROR>0
rollback tran t1--回滚事务
else
commit tran t1