SQL server3

/*创建存储过程
create proc Proc_Student
as
select * from tb_Student

执行存储过程:  
exec Proc_Student       
因为存储过程已经编译过,所以可以直接运行

查看存储过程的定义
sp_helptext 'Proc_Student'

修改存储过程(不会丢失与该存储过程的所有权限)
alter proc Proc_Student
as
select ID from tb_Student

修改存储过程名称
sp_rename 'Proc_Student', 'Proc_Stu'

删除存储过程
drop proc Proc_Student   
*/


/*创建一个DML触发器,在向表中插入数据时会提示
create trigger tri_Stu
on tb_Student
after insert
as raiserror('正在向表中插入数据', 16, 10)

执行下面操作时会提示服务器: 消息 50000,级别 16,状态 10,过程 tri_Stu                 
insert into tb_Student values(5, NULL, NULL, NULL)
DML事件触发DML触发器,DML事件是针对表或视图的insert update或delete 语句   
*/



/*
创建DDL触发器
create trigger trigger_DDL
on TEST
for drop_table
as
print '只有DDL触发器无效时,才可以删除表'
ROLLBACK

drop table tb_Student无法删除表
运用DDL触发器的四种场景:
1) 禁止用户修改和删除表;
2) 禁止用户删除数据库;
3) 记录和监控某数据库所有的DDL操作;
4) 把DDL操作信息以邮件的形式主动发送通知和预警;
*/


--登录触发器
-- use master,
-- go
-- create login sa with password='sa' must_change,
-- 	check_expiration= on;
-- go
-- grant view server state to sa;
-- go
-- create trigger dl_tri
-- on all server with exec as 'sa'
-- for logon
-- as
-- begin
-- if original_login()= 'sa' and
-- 	(select count(*) from sys.dx_exec_sessions
-- 		where is_user_process = 1 and
-- 			original_login_name= 'sa') > 1
-- rollback;
-- end; 


/*查看触发器:
exec sp_helptext 'tri_Stu'

修改触发器:
alter trigger tri_Stu
on tb_Student
after insert
as raiserror('正在向表中插入数据', 16, 10);
rollback

更改触发器名称
sp_rename 'tri_Stu', 'tri_stu'

禁用触发器:
disable trigger tri_stu on tb_Student

启用触发器
enable trigger tri_stu on student
*/


/*
索引的优点:
1.创建唯一性索引,保证表中每一行数据的唯一性
2.加快索引速度(主要原因)
3.加速表与表之间的连接
4.减少查询中分组和排序的时间
5.可以在查询过程中使用优化隐蔽器,提高系统性能

缺点:
1.创建和维护索引耗费时间,且随数据量增加而增加
2.索引需要占物理空间
3.对表数据进行增删改时,索引也要维护

创建非聚集索引:
use TEST
create index ix_Stu_ID
on tb_Student(ID)

创建唯一聚集索引:
use TEST
create unique clustered index ix_Stu_ID2
on tb_Student(ID)

查看表的索引:
exec sp_helpindex tb_Student

删除索引:
drop index tb_Student.ix_Stu_ID
*/



-- 设置索引的选项:
-- 1设置PAD_INDEX选项:设置创建索引期间中间级别页中可用空间的百分比
-- use TEST
-- create unique clustered index ix_stu_ID
-- on tb_Student(ID)
-- with pad_index, fillfactor= 10
-- 
-- 2设置FILLFACTOR选项:设置创建索引期间每个索引页的页级别中可用空间的百分比
-- use test
-- go
-- create index ix_stu_ID on tb_Student(ID)
-- with fillfactor= 10
-- go

--3.设置ASC/DESC选项:将查询结果按指定属性的顺序排列
--create clustered index ix_stu_ID
--on tb_Student(ID DESC)

--4.设置drop_existing选项:删除和重新创建现有索引
-- create unique clustered index ix_stu_ID
-- on tb_Student(ID)
-- with drop_existing

-- 使用showplan语句显示查询语句的执行信息
-- use TEST
-- go 
-- set showplan_all on
-- go
-- select ID, name from tb_Student where sex='男'
-- go
-- set showplan_all off
-- go
-- 
-- 使用statistics io显示有关由T-SQL语句生成的磁盘活动量的信息 
-- use TEST
-- go 
-- set statistics io on
-- go
-- select ID, name from tb_Student where sex='男'
-- go
-- set statistics io off
-- go

-- 清除索引上的碎片:
-- go
-- dbcc indexdefrag(TEST, tb_Student, ix_stu_ID)
-- go

--指定数据库进行全文索引
--exec sp_fulltext_database 'enable'
-- 删除全文索引
-- exec sp_fulltext_database 'disable'

--全文目录为虚拟对象,不属于任何文件组,它是一个表示一组全文索引的逻辑概念
--创建一个空的全文目录QWML:
-- go
-- exec sp_fulltext_database 'enable'
-- exec sp_fulltext_catalog 'QWML', 'create'

--返回有关全文目录QWML的信息
-- exec sp_help_fulltext_catalogs 'QWML';
-- gostatus


--游标由5部分组成:声明游标,打开游标,从一个游标中查找信息,关闭游标,释放游标
--游标的类型:静态游标,动态游标,只进游标,键集驱动游标

--创建一个名为Cur_Emp的标准游标
-- declare Cur_Emp cursor for
-- select * from tb_Student
-- go
-- 
-- 创建一个只读游标
-- declare Cur_Emp_1 cursor for
-- select * from tb_Student
-- for read only
-- go

--打开游标
-- open Cur_Emp_1
-- 
-- 打开游标之后,可以使用fetch命令读取游标的某一行数据
-- fetch next from Cur_Emp_1
-- while @@fetch_status = 0
-- begin
-- fetch next from Cur_Emp_1
-- end
-- 
-- 关闭游标:
-- close Cur_Emp_1
-- 
-- 释放游标:
-- deallocate Cur_Emp_1
-- 
-- 使用系统过程查看游标:
-- sp_cursor_list用来报告当前为连接打开的服务器游标的属性
-- sp_describe_cursor用于报告服务器游标的属性
-- use test
-- go
-- declare Cur_tb cursor for
-- select name
-- from tb_Student
-- where name like '李%'
-- open Cur_tb
-- declare @report cursor
-- exec master.dbo.sp_cursor_list @cursor_return= @report output,
-- 	@cursor_scope= 2
-- fetch next from @report
-- while(@@fetch_status <> -1)
-- begin fetch next from @report
-- end
-- close @report
-- deallocate @report
-- go
-- close Cur_tb
-- deallocate Cur_tb 
-- go

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值