57
Principle and Application of Dat abase System AnQing Teachers College Department of Computer & Information 数数数数数数数数 Principle and Application of Databas e system 数数数数数数数数数数数数数数

AnQing Teachers College Department of Computer & Information

Embed Size (px)

DESCRIPTION

AnQing Teachers College Department of Computer & Information. 数据库原理与应用 Principle and Application of Database system. 安庆师范学院计算机与信息学院. 第 9 章 查询. 9.1 单表查询 9.2 连接查询 9.3 嵌套查询. 查询:从数据库中获得所需要的数据。 查询利用 SELECT 语句实现。. 语句格式 SELECT [ALL|DIsTINCT] < 目标列表达式 >[,< 目标列表达式 >] … [INTO 新表名 ] - PowerPoint PPT Presentation

Citation preview

Page 1: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

AnQing Teachers College Department of Computer & Information

数据库原理与应用Principle and Application of Database system

安庆师范学院计算机与信息学院

Page 2: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

第 9 章 查询 9.1 单表查询 9.2 连接查询 9.3 嵌套查询

Page 3: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

查询:从数据库中获得所需要的数据。 查询利用 SELECT 语句实现。

Page 4: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

语句格式SELECT [ALL|DIsTINCT] < 目标列表达式 >[,< 目标列表达式>] …[INTO 新表名 ]FROM < 表名 >[ , < 表名 > ] …[ WHERE < 条件表达式 > ][ GROUP BY < 列名 1> [,< 列名 2>]][ HAVING < 条件表达式 > ][ ORDER BY < 列名 3> [ AsC|DEsC ] [,< 列名 4>[ AsC|DEsC ] ]]

Page 5: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

SELECT 子句:指定要显示的属性列INTO 子句:将查询到的结果集形成一个新表FROM 子句:指定查询对象 ( 表 )WHERE 子句:指定查询条件GROUP BY 子句:对查询结果按指定列进行分组,该属性列值相等的元组为一个组。HAVING 子句:筛选出只有满足指定条件的组ORDER BY 子句:对查询结果表按指定列值的升序或降序排序

Page 6: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

主要内容数据库的查询包括: 单表查询 连接查询 嵌套查询

Page 7: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

school 数据库student(sno,sname,ssex,sbirthday,class)teacher(tno,tname,tsex,tbirthday,prof,depart)course(cno,cname,tno)score(sno,cno,degree)

示例数据库

Page 8: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

9.1 单表查询

查询仅涉及一个表,是一种最简单的查询操作一、选择表中的若干列二、选择表中的若干行三、对查询结果排序四、使用集函数五、对查询结果分组 六、 HAVING 子句

Page 9: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

一、选择表中的若干列

1 查询指定列2 查询全部列3 修改查询结果中的列标题4 替换查询结果中的数据5 查询经过计算的值

Page 10: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

1. 查询指定列[ 例 1] 查询全体学生的学号与姓名。

SELECT sno,snameFROM student [ 例 2] 查询全体学生的姓名、学号、班号。SELECT sname,sno,classFROM student

Page 11: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

2. 查询全部列[ 例 3] 查询全体学生的详细记录。

SELECT sno , sname , ssex , sage , sdept FROM student 或SELECT *FROM student

Page 12: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

3. 修改查询结果中的列标题【例 4 】 查询 student 表中所有记录,结果中各列的标题分别指定为学号、姓名、性别、出生日期、班号。

SELECT sno AS ' 学号 ', sname AS ' 姓名 ',ssex AS' 性别 ', sbirthday AS ' 出生日期 ',class AS ' 班号 ' FROM student

' 学号 '=sno sno ' 学号 '

Page 13: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

4. 替换查询结果中的数据CASE

WHEN 条件 1 THEN 表达式1

WHEN 条件 2 THEN 表达式 2 . . . ELSE 表达式 nEND

Page 14: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

【例 5 】查询 score 表 sno,sname,degree 列,对 degree 列按以下规则进行转换;若 degree 为 90 ~ 100 ,替换为“优秀”,若 degree 为 80 ~ 89 ,替换为“良好”,若 degree 在 70 ~ 79之间,替换为“中等”,若 degree为 60 ~ 69 之间,替换为“及格”,若 degree 为 0 ~ 59 之间,替换为“不及格”,列标题更改为“ evaluation” 。

Page 15: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

SELECT sno, cno, evaluation= CASE WHEN degree>=90 AND degree<=100 THEN ' 优秀 ' WHEN degree>=80 and degree<=89 THEN ' 良好 ' WHEN degree>=70 and degree<=79 THEN ' 中等 ' WHEN degree>=60 and degree<=69 THEN ' 及格 ' ELSE ' 不及格 ' END FROM score

Page 16: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

5. 查询经过计算的值 SELECT 子句的 < 目标列表达式 > 为表达

式 算术表达式 字符串常量 函数

Page 17: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

[ 例 6] 查全体学生的姓名及其年龄。SELECT sname,'age:',2000-DATEPART(yy,sbirthday)FROM student

Page 18: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

二、选择表中的若干行1 消除结果集中的重复2 限制结果集的返回行数3 查询满足条件的行 (1)表达式比较 (2) 指定范围 (3) 确定集合 (4) 字符匹配 (5) 空值比较 (6) 多重条件查询

Page 19: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

1. 消除结果集中的重复行[ 例 7] 查询选修了课程的学生学号。

(1) SELECT sno

FROM score

或 ( 默认 ALL)

SELECT ALL sno

FROM score

ALL 与 DISTINCT

Page 20: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(2) SELECT DISTINCT sno

FROM score

Page 21: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

2. 限制结果集的返回行数可使用 TOP 选项限制查询结果集返回的行数。其基本格式为:TOP n [PERCENT]

[ 例 8]

SELECT TOP 4 * FROM score

SELECT TOP 40 PERCENT * FROM score

Page 22: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

3. 查询满足条件的行WHERE 子句常用的查询条件

查 询 条 件 谓 词比 较 =,>,<,>=,<=,!=,<>,!>,!<;

确定范围 BETWEEN …AND…,NOT BETWEEN… AND…确定集合 IN ,NOT IN字符匹配 LIKE ,NOT LIKE空 值 IS NULL ,IS NOT NULL多重条件 AND ,OR

Page 23: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(1) 比较大小在 WHERE 子句的 < 比较条件 > 中使用比较运算符

= , > , < , >= , <= , != 或 <> , !> , !< , 逻辑运算符 NOT + 比较运算符[ 例 9] 查询所有在’ 1975-01-01’ 后出生的学生学号 及其姓名。 SELECT sno,sname FROM student WHERE sbirthday>’1975-01-01’ 或

SELECT sno,snameFROM student WHERE NOT sbirthday <= '1975-01-01'

Page 24: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(2) 确定范围 使用谓词 BETWEEN … AND … NOT BETWEEN … AND …[ 例 10] 查询成绩在 60 ~ 80 (包括 60 分和 80

分)之间的所有记录。SELECT * FROM scoreWHERE degree BETWEEN 60 AND 80

Page 25: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)[ 例 11] 查询成绩不在 60 ~ 80 之间的所有记录。

SELECT * FROM scoreWHERE degree NOT BETWEEN 60 AND 80

Page 26: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(3) 确定集合 IN <取值表 >, NOT IN <取值表 >

<取值表 > :用逗号分隔的一组取值[ 例 12] 查询成绩为 85 、 86 或 88 的记录。

SELECT * FROM score WHERE degree IN(85,86,88)

Page 27: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(3) 确定集合[ 例 13] 查询成绩既不是 85 、 86 ,也不

是 88 的记录。SELECT * FROM score WHERE degree NOT IN(85,86,88)

Page 28: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(4) 字符串匹配 [NOT] LIKE ‘< 匹配串 >’ [ESCAPE ‘ < 换码字符

>’]< 匹配串 > :固定字符串或含通配符的字符串

当匹配串为固定字符串时: 可以用 = 运算符取代 LIKE 谓词 用 != 或 < > 运算符取代 NOT LIKE 谓词

Page 29: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题1) 匹配串为固定字符串 [ 例 14] 查询学号为 101 的学生的详细情况。

SELECT * FROM student WHERE sno LIKE ‘101'等价于: SELECT * FROM student WHERE sno = ‘101'

Page 30: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

通配符 % (百分号 ) 代表任意长度(长度可以为 0)的字符串

例: a%b 表示以 a开头,以 b 结尾的任意长度的字符串。如 acb , addgb , ab 等都满足该匹配串

_ ( 下划线 ) 代表任意单个字符 例: a_b 表示以 a开头,以 b 结尾的长度为 3 的任意

字符串。如 acb , afb 等都满足该匹配串

Page 31: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)2) 匹配串为含通配符的字符串[ 例 15] 查询所有姓王学生的姓名、学号和性别。

SELECT sname,sno,ssex FROM student WHERE sname LIKE '王%'

Page 32: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)[ 例 16] 查询姓“李”且全名为二个汉字的学生的姓名。 SELECT sname FROM student WHERE sname LIKE '李 _'

Page 33: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)[ 例 17] 查询名字中第 2 个字为 "阳 " 字的学生的姓名和学号。

SELECT sname,sno FROM student WHERE sname LIKE '_阳%'

Page 34: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)[ 例 18] 查询所有不姓李的学生姓名。

SELECT sname FROM student WHERE sname NOT LIKE ‘李%'

Page 35: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

ESCAPE 短语:当用户要查询的字符串本身就含有 %

或 _ 时,要使用 ESCAPE '< 换码字符>' 短语对通配符进行转义。

Page 36: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)3) 使用换码字符将通配符转义为普通字符 [ 例 19] 查询课程名为 DB_Design 课程的课程号和任课教师。 SELECT cno,tno FROM course WHERE cname LIKE 'DB\_Design' ESCAPE '\'

Page 37: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题(续)使用换码字符将通配符转义为普通字符(续 )[ 例 20] 查询以 "DB_" 开头,且倒数第 3 个字符为 i 的课程的详细情况。 SELECT * FROM course WHERE cname LIKE 'DB\_%i_ _' ESCAPE ' \ '

Page 38: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(5) 涉及空值的查询 使用谓词 IS NULL 或 IS NOT NULL “IS NULL” 不能用 “ = NULL” 代替

[ 例 21] 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。

SELECT sno,Cno FROM score WHERE degree IS NULL

Page 39: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题 ( 续 ) [ 例 22] 查所有有成绩的学生学号和课程号。 SELECT sno,cno FROM score WHERE degree IS NOT NULL

Page 40: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

(6) 多重条件查询用逻辑运算符 AND 和 OR来联结多个查询条件

AND 的优先级高于 OR 可以用括号改变优先级

Page 41: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题[ 例 23] 查询 95033 班且在’ 1975-01-01’ 后出生的学生姓名。 SELECT snameFROM studentWHERE class= '95033' AND sbirthday>'1975-01-01'

Page 42: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

改写 [ 例 10][ 例 10] 查询成绩在 60 ~ 80 (包括 60 分和 80 分)之间的所有记录。SELECT * FROM scoreWHERE degree BETWEEN 60 AND 80可改写为:SELECT *FROM scoreWHERE degree>=60 AND degree<=80

Page 43: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

改写 [ 例 12][ 例 12] 查询成绩为 85 、 86 或 88 的记录。

SELECT * FROM score WHERE degree IN(85,86,88)

可改写为:SELECT *FROM scoreWHERE degree=85 OR degree=86 OR degree=88

Page 44: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

三、对查询结果排序 使用 ORDER BY子句

可以按一个或多个属性列排序 升序: ASC 降序: DESC缺省值为升序

当排序列含空值时 ASC :排序列为空值的元组最先显示 DESC :排序列为空值的元组最后显示

Page 45: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

对查询结果排序(续) [ 例 24] 查询选修了 3-105 号课程的学生的学号及其成绩,查询结果按分数降序排列。 SELECT sno,degree FROM score WHERE cno= '3-105' ORDER BY degree DESC

Page 46: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

对查询结果排序(续) [ 例 25] 查询全体学生情况,查询结果按所在班的班号升序排列,同一班中的学生按出生先后顺序排列。

SELECT * FROM student ORDER BY class,sbirthday

Page 47: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

四、使用集函数 5类主要集函数

计数COUNT ( [DISTINCT|ALL] *)COUNT ( [DISTINCT|ALL] < 列名 >)

计算总和SUM ( [DISTINCT|ALL] < 列名 >)

计算平均值AVG( [DISTINCT|ALL] < 列名 >)

Page 48: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用集函数(续) 求最大值

MAX( [DISTINCT|ALL] < 列名 >) 求最小值MIN ( [DISTINCT|ALL] < 列名 >)

– DISTINCT短语:在计算时要取消指定列中的重复值– ALL短语:不取消重复值– ALL 为缺省值

Page 49: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用集函数 (续)[ 例 26] 查询学生总人数。

SELECT COUNT(*) FROM student [ 例 27] 查询选修了课程的学生人数。 SELECT COUNT(DISTINCT sno) FROM score

注:用 DISTINCT 以避免重复计算学生人数

Page 50: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用集函数 (续)[ 例 28] 计算 3-105 号课程的学生平均成绩。

SELECT AVG(degree) FROM score WHERE Cno= '3-105' [ 例 29] 查询选修 3-105 号课程的学生最高分数。 SELECT MAX(degree) FROM score WHER cno= '3-105'

Page 51: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

五、对查询结果分组 使用 GROUP BY子句分组 细化集函数的作用对象

未对查询结果分组,集函数将作用于整个查询结果

对查询结果分组后,集函数将分别作用于每个组

Page 52: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用 GROUP BY 子句分组[ 例 30] 求各个课程号及相应的选课人数。 SELECT cno,COUNT(*) ' 选课人数 ' FROM score GROUP BY cno 结果 cno 选课人数

3-105 6

3-245 3

6-166 3

Page 53: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

对查询结果分组 (续) GROUP BY 子句的作用对象是查询的中间结果

分组方法:按指定的一列或多列值分组,值相等的为一组

使用 GROUP BY 子句后, SELECT 子句的列名列表中只能出现分组属性和集函数

Page 54: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用 HAVING 短语筛选最终输出结果[ 例 31] 查询选修了 2门或 2门以上课程的学生学

号。 SELECT sno FROM score GROUP BY sno HAVING COUNT(*) >=2  

Page 55: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

例题[ 例 32] 查询有 2门或 2门以上课程都至少为 85

分的学生的学号及其课程数。 SELECT sno, COUNT(*) FROM score WHERE degree>=85 GROUP BY sno HAVING COUNT(*)>=2

Page 56: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

此语句执行过程如下:1. ( FROM)取出整个 score2. ( WHERE)筛选 Grade>=85 的元组3. ( GROUP BY)将选出的元组按 sno 分组4. ( HAVING)筛选选课 2门或 2门以上的分组5. ( SELECT)以剩下的组中提取学号和课程数

Page 57: AnQing Teachers College  Department of Computer & Information

Principle and Application of Database System

使用 HAVING 短语筛选最终输出结果 只有满足 HAVING短语指定条件的组才输出 HAVING短语与 WHERE 子句的区别:作用对象不同

WHERE 子句作用于基表或视图,从中选择满足条件的元组。 HAVING短语作用于组,从中选择满足条件

的组。