数据库_数据查询和更新

【说在前面的话】

对查询语句来说,要查询什么,那么这个列名就要放在select 后面,那么如果查询的列不在同一个表中,可以选择表连接,也可以选择嵌套

要查询的内容在哪个表中,from 后面就要写哪个表

后面的部分就是条件,通常where, 也有group by 等其他语句

【下表格为常用查询条件】

查询条件 谓词
比较  =, >, <, >=, <=, !=, <>, !>, !<;   not+上述比较运算符
确定范围 between and, not between and
确定集合 in, not in
字符匹配 like, not like
空值 is null, is not null
多重条件(逻辑运算) and, or, not

【下面列出SQL提供的聚集函数】

count    统计个数

sum    求和

avg    求平均值

max    求最大值

min    求最小值


下面通过具体内容,来讲解数据查询相关操作

首先放我建得学生表,表名分别为student、course、sc






查询全体学生的学号、姓名,要求查询结果按学号的升序进行排序

select sno, sname from student order by sno asc;
//order by 子句对查询结果按照一个或多个属性列的升序(asc)或降序(desc)排列,默认值为升序

查询成绩低于70分的学生学号、课程号、课程名和成绩,并在查询结果中给出临时列标题

select sno, sc.cno, grade, cname from sc, course where sc.cno = course.cno and grade < 70 ;
//值得注意的是此查询结果并不在同一个表内,那么我们就需要指定查询结果是哪个表中的,如此题的sc.cno,在输入条件时我们需要将表相连,连接条件就是两表共同的cno

查询选修了4号课程且成绩在70-80分之间的学生学号

select sno from sc where cno = 4 and grade >= 70 and grade <= 80;

查询选修了3号课程且成绩在70分以上的学生学号和成绩

select sno from sc where cno = 3 and grade >= 70 ;

查询“计算机系”和“数学系”学生的全部信息

select * from student where sdept = 'cs' or sdept = 'math';
select * from student where sdept IN ('cs', 'math');            //两种写法均可

查询选修了3号课程的学生学号和成绩,显示成绩加上10分以后的成绩,并给出临时标题

select sno as 学号, grade+10 as 成绩 from sc where cno = 3 ;
//列临时标题,就是将结果列明重命名,在要查询的结果后面加上as xxx,xxx便是临时名

查询STUDENT表中前5个记录

select top 5 *from student;        *表示全部信息

查询所有姓“王”的学生信息

select * from student where sname like 'w%';
//首先在字符匹配中,要用like,如果like后面的匹配串中不含通配符,则可以用=运算符取代like谓词用 !=   或<>(不等于)运算符取代not like谓词
  其次%代表任意长度(长度可以为0)的字符串

查询全体学生的姓名及出生年份

select sname, 2018-sage from student;
//用今年的年份-sage,变得出年龄

查询课程名中包含“DB_”的课程信息

select * from course where cname LIKE '%DB_%';
//包含DB_的课程名,那么也就代表前后都可以有任意长度字符串

查询所有成绩为空的学生的学号、姓名、选修课程号和课程名

select sc.sno, sname, sc.cno, cname 
from sc, student, course 
where sc.cno = course.cno and sc.sno = student.sno and grade is NULL;
//首先,要查询的内容在三个表格中,那么在表格之间重复的列,例如本题我们要指定查询哪个表中的cno或  sno
  其次,进行表连接
  再其次,空值要用is null

查询全体学生的信息,查询结果按所在系的系名升序排列,同一系的学生按年龄降序排列

select * from student order by sdept asc, sage desc;

查询所有学生的选课信息,要求列出学生学号、姓名、选修课程名和成绩

select sc.sno, sname, cname, grade 
from sc, course, student
where sc.cno = course.cno and student.sno = sc.sno;

查询选修了3号课程的学生姓名和所在系。(用两种类型的查询实现)

select sname, sdept 
from student, sc 
where student.sno = sc.sno and cno = 3;


select sname, sdept
from student
where sno in (select sno from sc where cno = 3);
//在嵌套中,确定集合用in 并且in前面的和嵌套语句中select后面的一定是相同的没跑

查询选修了“数据库原理及应用”课程并且成绩在80分以上的学生姓名。(用两种类型的查询实现)

select sname 
from student, sc, course
where sc.cno = course.cno and sc.sno = student.sno and cname = '数据库' and grade > 80;
查询全体学生的学号、姓名、选修课程号、课程名和成绩信息。查询结果先按照学号的升序排列,后按照课程号的升序排列
select sname
from student 
where sno in 
(select sno from sc where grade > 80 and 
	cno in (select cno from course where cname = '数据库'));
查询“计算机系”学生的学号、姓名、选修课程号、课程名和成绩信息
select sc.sno, sname, sc.cno, cname, grade 
from sc, course, student
where sdept = 'cs' and sc.sno = student.sno and sc.cno = course.cno;

查询名字中第2个字为“小”或“大”字的学生姓名和学号

select sname, sno from student where sname like '_小%' or sname like '_大%';
//_代表任意单个字符

统计每个系的学生人数

select sdept, count(sno) from student group by sdept;
//group by 子句将查询结果按某一列或多列的值分组,值相等的为一组

统计每门课程的选课人数

select cno, count(cno) from sc group by cno;

统计平均成绩大于70分的课程名

select cname 
from course 
where cno in(	select cno 
		from sc
		group by cno 
		having avg(grade)>70);
//having 短语给出了选择组的条件,只有满足条件(即平局分大于70)的组才会被选出来

查询选修了3门以上课程的学生的学号

select sno from sc group by sno having count(*)>3;

统计选修课程总分大于150分的学生的学号及总成绩

select sno, sum(grade) from sc group by sno having sum(grade)>150;

将“计算机系”不及格学生的成绩改为60分

update sc set grade = 60 where grade < 60;

删除“数学系”不及格学生的选课记录

delete from sc where grade < 60 and 
				sno in(
				select sno 
				from student 
				where sdept = 'math' );

查询哪些课程没有人选修,要求列出课程号和课程名(使用外连接)

select cno, cname from course where cno not in(select cno from sc);

查询每一门课的先行课

select first.cno, second.cpno from course first, course second where first.cpno = second.cno;  //cpno表示先行课
//这个操作属于表与自身进行连接,那么我们需要将课程表取两个别名去区分它们 姑且叫他们first和second

猜你喜欢

转载自blog.csdn.net/LJH_laura_li/article/details/80384298