数据库 SQL 2005 知识点三

来源:互联网 发布:对于上海踩踏事件知乎 编辑:程序博客网 时间:2024/06/07 02:22

单表查询分为 

投影从列的角度

选择 从行的角度

连接 从行和列

创建虚表

select 列名

into 新表名 from 原表

多表查询 

嵌套查询,连接查询。

1、select 列名1,列名2,列名3, from 表名

2、select sno as ‘学号’,sname as '姓名‘ from sc

3、select top 4*  from sc 

4、use school select sno,degree 

from sc 

where cno='3-105'

order by degree desc(降序)

5、select * 

from student 

order by class ,sbirthday

6、比较搜索条件

select sno,sname 

from student 

where sbirthday >'1975-01-01' 

7、范围搜索条件

select * 

from sc

where degree between 60 and 80  (包括左右两个边界)

8、列表搜索条件 

select *

from sc

where degree in (86,89,89)

9、搜索条件中的模式匹配

select *

from student 

where sno like "  " 

通配符   含义

%    包含零个或是多个字符的任意字符  例如 王% 王在前的任意字符,%王% ,王的任意位置

涉及空值得查询

使用谓词 is null 或者is not null 

多重条件查询,

select * from sc where degree >=60 and degreee<=80

10、 聚合函数

 select count (*) 

from student 

select count (distinct sno )

from sc 

select avg(degree)

from sc 

where cno='3-105'

11、数据分组

 select cno ,cout(*) as '选课人数'

from sc 

group sc 

 select sno 

from sc 

group by sno 

having cout(*)>=2

查询选修了2门和2门以上课程都至少为85分的学生学号以及课程书

select sno ,count(*)

from sc  

where degree>=85 

group by sno 

having count(*)>=2

多表查询的子查询 分为嵌套查询和连接查询

select sno,sname 

from student 

where sno in (select sno 

from sc where cno='3-105')

带有in谓词的子查询 in返回的是一个集合

查询与‘王芳’在同一班级学习的学生详细信息

select*

from student 

where class in (select class from student where sname=' 王芳’)

查询选修课名为‘操作系统’的学生学号和姓名

select student .sno ,sname

from student 

where sno in (select sno from sc where cno in (select cno from course where canme='操作系统‘))  个人建议用三表连接会简单

带有exists的子查询

只产生逻辑真值true或逻辑假值false,若内层查询结果为非空,则返回真值,若内层查询结果为空,则返回假值

查询所有选修了3-245 课程的学生姓名

select sname 

from student 

where exists (select * from sc where sc.sno=student.sno and cno='3-245')

表内连接 两种写法

select student *,sc.*

from student,sc 

where student.sno=sc.sno

select student*,sc.*

from student join sc on student.sno=sc.sno

表自身连接

select x.*,y.* from sc x,sc.y

where x.cno='3-105' and y.cno='3-105'

and y.sno='109' and x.degree>y.degree 

order by x.degree desc 

表外连接

左连接:将左表的所有记录分别与右表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示左边表中不符合条件的数据行,此时右边数据行会以null来显示。select T1.*,T2.*  from T1 left outer join T2 on T1.sno =T2.sno 

右连接:将右表的所有记录分别与左表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示右边表中不符合条件的数据行,此时左边数据行会以null来显示。  select T1.*, T2.* from T1 right outer join T2 on T1.sno=T2.sno 

全外连接  select T1.*,T2.*  from T1 full join T2 on T1.sno=T2.sno

复合条件连接 多个表

查询选修3-105 号课程且成绩在90 分以上的所有学生的学号,姓名

select student sno,student.sname 

from student sc

where student .sno=sc.sno and sc.cno='3-105' and sc.degree>90

方法二  select student.sno ,student.sname 

from student join sc on student.sno=sc.sno and sc.cno='3-105'and sc.degree>90

多表连接 

select  student.sno ,sname ,cname,degree 

from student ,sc, course

where student,sno=sc,sno and sc.cno=course.cno

select student.sno ,sname ,cname ,degree

from student join sc on sutdent.sno=sc.sno  join course on sc.cno=course.cno 

from A  join B on 连接条件1 join C on 连接条件2 

使用update 修改数据

 update student 

set sname='新名’

where sno='101' 

update  sc

 set  degree=degree+5

删除表  delete  student  from student where sname=' 王二小‘

0 0
原创粉丝点击