4.1
4.1.1SQL语言的特点:一体化,高度非过程化,面向集合的操作方式,提供多种方式使用,语言简洁
4.1.2SQL语言的功能:数据定义,数据查询,数据操纵,数据控制
4.2SQL语句支持的数据类型:数值型,日期时间型,字符串型,其他类型
4.3数据定义功能
1.数据库创建:create database 数据库名
2.数据库修改:alter database 数据库名
3.数据库删除:drop database 数据库名
4.表的创建:create table 表名
not null:非空约束,限制列取值非空
primary key:主键约束,指定本列为主键
foreign key:外键约束,定义本列为引用其他表的外键
unique:唯一值约束,限制列取值不能重复
default:默认值约束,指定列的默认值
check:列取值范围约束,限制列的取值范围
4.4数据查询功能
select:指定输出的字段
from:用于指定数据的来源
where:用于指定数据的行选择条件(其子句常用到的查询条件
(1)比较:=,<=,>=,<>,<,>,!=
(2)确定范围:between···and , not between···and
(3)确定集合:in,not in
(4)字符匹配:like,not like(匹配任意一个字符:_;匹配0到多个字符:%;匹配[ ]中任意一个字符:[ ])
(5)空值:is null,isnot null
(6)多重条件:and,or
group by:用于对检索到的记录进行分组
having:用于指定对分组后结果的选择条件
order by:用于对查询的结果进行排序
count(*):统计表中元组个数
count([distinct]列名):统计本列的列值个数,distinct表示去掉重复的后统计
sum 列名:计算列值的和值(必须是数值型)
avg 列名:计算列值的数值型(必须是数值型)
max 列名:得到列值的最大值
min 列名:得到列值的最小值
多表连接查询:
内连接:from 表1 inner join 表2 on 连接条件
外连接:from 表1 left right outer join 表2 on 连接条件(left outer join为左外连接,同理···)
top的使用:
n是非负整数
top n: 取查询结果的前n行数据
top n persent:取查询结果的前%n行数据
where ties:包括并列的结果
实例展示:
1.查询全体学生的姓名和学号
select sno,sname from student
2.查询全体学生的全部信息
select * from student
3.查询全体学生的姓名和年龄
select sname,year(getdate())-year(sbirthday) from student
4.查询寻全体学生姓名,年龄,字符串“今年是”以及今年的年份
select sname 姓名,year(getdate())-year(sbirthday) 年龄, '今年是‘ 今年是,year(getdate()) 年份 from student
5.查询计算机系全体学生的姓名
select sname from student
where sdept="计算机系"
6.查询考试成绩大于90的学生的学号,课程号和成绩
select sno,cno,grade from sc
where grade>90
7.查询学分在2~3之间的课程的课程名称,学分和开课学期
select cname,credit,semester from course
where credit betweent 2 and 3 (where credit>=2 and credit<=3)
8.查询学分不在2~3之间的课程的课程名称,学分和开课学期
select cname,credit,semester from course
where credit not between 2 and 3 (where credit<2 or credit>3)
9.查询出生在1997年学生的全部信息
select * from student
where sbirthday between '1997-01-01' and '1997-12-31'
10.查询计算机系和机电系学生的学号,姓名和所在系
select sno,sname,sdept from student
where sdept in(’计算机系‘,'机电系’)
11.查询不在计算机系和机电系学生的学号,姓名和所在系
select sno,sname,sdept from student
where sdept not in(’计算机系‘,'机电系’)
12.查询姓李的学生的学号,姓名和所在系
select sno,sname,sdept from student
where sname like '李%‘
13.查询姓名中第二个字是”冲“的学生的学号,姓名,所在系
select sno, sname,sdept from student
where sname like "_冲%"
14.查询学号的最后一位不是”2“或”3“的学生的学号,姓名和所在系
select sno,sname,sdept from student
where sno not like '%[2,3]'
15.查询还没有考试的学生的学号,相应的课程号和成绩
select sno cno grade from sc
where grade is null
16.查询有备注的学生的学号,姓名,备注
select sno,sname,memo from student
where memo is not null
17.查询机电系有备注的学生的学号,姓名,所在系和备注
select sno,sname,sdept,memo from student
where sdept='机电系‘ and memo is not null
18.查询计算机系和机电系1997年出生的学生的学号,姓名,所在系和出生日期
select sno,sname,sdept,sbirthday from student
where (sdept='机电系’ or sdept='计算机系‘)
and (sbirthday between '1997-01-01' and '1997-12-31')
19.查询有考试挂科的学生的学号
select distinct sno from sc
where grade<60
20.将C01号课程的成绩按升序排列
select cno,grade from sc
where cno='C01' order by grade
21.将060101 号学生的成绩按降序排列
select cno,grade from sc
where sno='060101' order by grade desc
22.统计学生总人数
select count(*) 学生总人数 from student
23.统计学生060101的总成绩
select sum(grade)总成绩 from sc
where sno='060101'
24.统计学生’060101‘学生的平均成绩
select avg(grade)平均成绩 from sc
where sno='060101'
25.统计课程C01的最高分数和最低分数
select max(grade)最高分,min(grade)最低分 from sc
where cno='C01'
26.统计每门课程的选课人数,列出课程号和选课人数
select cno 课程号,count(sno) 选课人数 from sc group by cno
27.统计每个学生的选课门数,列出学号,选课门数和平均成绩
select sno,count(cno)选课门数,avg(grade)平均成绩 from sc groupt by cno
28.统计每个系的男生人数和女生人数,结果按系名的升序排列
select sdept,ssex,count(*)人数 from student
group by sdept,ssex order by sdept
29.统计每个系的男生人数
select sdept, count(*)男生人数 from student
where ssex='男’ group by sdept
30.查询选课门数超过3门的学生的学号和选课门数
select sno,count(*)选课门数 from sc
group by sno having count(*)>3
31.查询计算机系和机电系每个系的学生人数
(1)
select sdept, count(*)学生人数 from student
group by sdept
having sdept in ('计算机系‘,’机电系‘)
(2)
select sdept,count(*)学生人数
where sdept in ('机电系’ ,‘计算机系’)
group by sdept
32.查询每个学生及其选课的详细
select * from student inner join sc
on student.sno=sc.sno
33.查询每个学生及其选课的详细,要求去掉重复列
select s.sno,sname,ssex,sbirthday,sdept,memo,cno,grade
from student.s inner join sc on s.sno=sc.sno
34.查询计算机系选修了”数据库原理“课程的学生的成绩单,成绩单包含姓名,课程信息,成绩信息
select sname,cname,grade from student.s inner join sc on s.sno=sc.sno
join course c on c.cno=sc.cno
where sdept='计算机系‘ and cname='数据库原理’
35.查询选修了数据库原理课程的学生姓名和所在系
select sname,sdept from student s inner join sc on s.sno=sc.sno join course c on c.cno=sc.cno
where cname='数据库原理‘
36.统计每个系的学生的平均成绩
select sdept avg(grade)系平均成绩
from student s inner join sc on s.sno=sc.sno join course c on c.cno=sc.cno
group by sdept
37.统计计算机系学生中每门课的选课人数,平均分,最高分和最低分
select count(sno)选课人数,avg(grade)平均分,max(grade)最高分,min(grade)最低分
from student s inner join sc on s.sno=sc.sno
where sdept='计算机系’
group by cno
38.查询课程数据库原理的先修课程名
select c1.cname 课程名,c2.cname先修课程名
from course c1 join course c2 on c1.precno=c2.cno
where c1.cname="数据库原理”
39.查询与钟文辉在同一个系学习的学生的姓名和所在系
select s1.sname,s1.sdept
from student s1 join s2 on s1.sdept=s2.sdept
where s1.sname='钟文辉‘ and s2.sname!='钟文辉’
40.查询计算机系全体学生的选课情况
select s.sno,sname,sdept,sc.cno
from student s left join sc on s.sno=sc.sno
where sdept='计算机系‘
on s1.sdept=s2.sdept