SQL Server数据库新手入门学习总结(二)

3.查询
简单查询,使用TOP子句
查询结果排序order by
带条件的查询where,使用算术表达式,使用逻辑表达式,使用between关键字,使用in关键字,
模糊查询like
在查询中使用聚合函数:sum(x),avg(x),min(x),max(x),count(x),count()
使用分组查询group by,having子句
distinct关键字
列别名
select top 6
from sales order by qty desc
select au_id,au_fname,au_lname from authors where state in('ks','ca','mi')
select au_fname,au_lname,phone from authors where au_id like '72[234]-%'
select type,sum(price),avg(price),count(*) from titles group by type having type in('business','psycheology')

简单子查询:嵌套子查询、相关子查询;子查询的select语句中不能使用order by子句,roder by子句只能对最终查询结果排序。
嵌套子查询:执行过程,先执行子查询,子查询得到的结果不被显示,而是传给外层查询,作为外层查询的条件,然后执行外层查询,并显示结果。
嵌套子查询的执行不依赖于外层查询,子查询只执行一次。
带有比较运算符的子查询,带有in和not in的子查询,带有any或all的子查询
相关子查询:子查询为外层查询的每一行执行一次,外层查询将子查询引用的列的值传给了子查询。
相关子查询的执行依赖于外层查询,子查询需要重复的执行。
带有exists和not exists的相关子查询。
多表联接查询:内联接(inner join)、外联接((left、right、full)outer join)、自联接(self join)和交叉联接(cross join)
在查询上创建新表:select into语句首先创建一个新表,然后用查询的结果填充新表。
表别名
select coursename from course where courseid in(select distinct courseid from grade where grade>10)
select studname from student where sudbirthday > any (select studbirthday from student where class = '信息系') and class<>'信息系'
select studname from student where exists (select from grade where studid = student.studid and courseid = '01')
select stud1.
from student as stud1 join student as stud2 on stud2.studname = 'mm' and stud1.studsex = stud2.studsex
select * into girls from student where studsex='m'

4.视图、索引和事务
视图是由一个或多个数据表(基本表)导出的虚拟表或者查询表,是关系数据库系统提供给用户以多种角度观察数据库中数据的重要机制。
视图的好处:能够简化用户的操作;视图能够对机密数据提供安全保护。
创建视图时,视图的名称存在sysobjects表中。有关视图中所定义列的信息添加到syscolumns表中,而有关视图相关性的信息添加到sysdepends表中。另外,create view语句的文本添加到syscomments表中。
在通过视图向表中插入数据时,如果insert语句列表中包含有视图中没有选择的列和不允许为空值的列,这种操作是不允许的。
创建视图:create view view_employee as select emp_id,fname,lname from employee
使用视图:select * from view_employee
修改视图:alter view view_employee as select emp_id,fname,job_id from employee where job_id>10
删除视图:drop veiw view_employee
查看视图结构:exec sp_help view_employee
查看视图定义信息:exec sp_helptext 'view_employee'

索引提供了一种基于一列或多列的值对表的数据行进行快速访问的方法。索引提供的是表中得逻辑顺序。
聚集索引基于数据行的键值在表内排序和存储这些数据行。当数据表以某列为关键字建立聚集索引时,表中得数据行就以该列(聚集索引键)的排序次序进行存储。每个表只能有一个聚集索引。
非聚集索引具有完全独立于数据行的结构,一个表可以建立多个非聚集索引。
创建聚集索引:create clustered index studid_ind on stud(studid)
创建非聚集索引:create unique index studfullname_ind on stud(fname desc,lname)
删除索引:drop index stud.studid_ind
查看stud表上得索引:exec sp_helpindex stud

事务是一种机制,是一个操作序列,它包含了一组数据库操作命令,并且所有的命令作为一个整体一起向系统提交或撤销操作请求。
事务的特性:原子性(Atomicity)、一致性(Consistenty)、隔离性(Isolation)、永久性(Durability)。
事务分类:显示事务、隐性事务、自动提交事务。

视图、索引和事务的创建、使用、修改和删除

本文转载,有疑问请联系 http://imtcf.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章