20151016: --触发器:一种特殊的存储过程,通过对数据库表操作的 --动作,来触发,增删改 alter trigger Fruit_Insert_After --修改用alter on fruit --对哪个表操作的时候执行触发器 instead of insert -- for --(update,delete) for的意思是动作之后触发after等同 as select *from inserted --inserted临时表,就是增加的数据 go select *from student select *from score delete from student where sno=101--有主外键关系 alter trigger student_delete_instead on student instead of delete as --先删除外键表数据 delete from score where sno in( select sno from deleted ) --再删主键表 delete from student where sno in (select sno from deleted) Go --分页语句:查询-10的数据(尽量用主键唯一查询) select top 5 *from car order by price desc go select top 5 *from car where code not in (select top 5 code from car) go select top 5 * from car where code not in (select top 10 code from car) go use shujuData go select * from score go select top 5 a.sno,a.cno,a.degree from Score as a left join (select top 5 *from Score) as b on a.sno=b.sno and a.cno=b.cno where b.sno is null--判断是否为空is --事务:出现错误时,会返回最开始的过程; --保障流程的完整执行,如果一步失败就会返回到起点 select *From student select *From score begin tran --在流程开始的位置 delete from student where sno=‘304‘ delete from Student where Sno=‘303‘ if @@ERROR>0--看全局变量里是否有错误记录 begin rollback tran--回滚事务 end else begin commit tran--提交事务 end
时间: 2024-12-18 06:15:39