SQL Server数据库入门学习总结

2020-07-10 08:06:39易采站长站整理

数据查询语言DQL(Data Query Language)用来查询数据库中得数据的命令:select。
SQL中得运算符:算术运算符、位运算符、比较运算符、逻辑运算符、通配运算符、字符串连接符、赋值运算符
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.视图、索引和事务
视图是由一个或多个数据表(基本表)导出的虚拟表或者查询表,是关系数据库系统提供给用户以多种角度观察数据库中数据的重要机制。
视图的好处:能够简化用户的操作;视图能够对机密数据提供安全保护。
相关文章 大家在看