实验四视图和索引
实验目的:
掌握视图的定义、查询和更新;
掌握索引的定义;
掌握利用索引优化查询效率;
实验原理:
定义视图
CREATE VIEW <视图名> [(<列名> [,<列名>]…)]
AS <子查询>
[WITH CHECK OPTION];
删除视图
DROP VIEW <视图名>
(注意:索引不能修改)
定义索引
CREATE [UNIQUE][CLUSTER] INDEX <索引名>
ON <表名> (<列名>[<次序>][,<列名>[<次序>]]…);
删除索引
DROP INDEX <索引名>
实验方法:
将查询需求用T-SQL语言表示;在SQL Server Query Analyzer的输入区中输入T-SQL查询语句;设置 Query Analyzer的结果区为Standard Execute(标准执行)或Execute to Grid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。
实验内容:
1、merce数据库中完成如下问题:
为表Employees在Title列上添加一个升序索引;
为表orders添加索引v1,索引键OrderDate 升序,ShippedDate 降序;
设计一个视图View1,用于查询每个员工签订的订单总数;
设计视图View2,查询ShipCountry为Canada,且订购货物数量超过30件的订单信息
在View2的基础上设计视图View3,查询ShipCountry为Canada,且订购货物数量超过30件的顾客信息;
删除视图View2,测试View3是否仍有效;
2、测试索引测试索引
实验步骤:
(1)create
index aa on Employees(Title asc)
(2)create
index vi on orders(OrderDate asc,ShippedDate desc)
sp_helpindex Employees
sp_helpindex orders
(3) create view view1
as
select EmployeeID,sum(Quantity) as mount
from orders
group by EmployeeID
(4) create view view2
as
select distinc Orders.* from Orders,Customers
where ShipCountry='Canada'
and Quantity>30
(5) create view view3
as
select CustomerID,sum(quantity) as 数量 from view2
where ShipCountry='Canada' and
Quantity>30
group by CustomerID
(6)
drop view view2
select * from view3
实验五数据安全性
实验目的:
本实验的目的是通过实验使学生加深对数据安全性的理解,并掌握SQL Serve
数据库视图和索引 来自淘豆网www.taodocs.com转载请标明出处.