一、 数据查询

1.1 单表查询

1、查询学生表中的全部信息

SELECT * from student;

2、对指定信息进行查询(学生姓名,性别,年龄)

select sname,ssex,sage from student;

 3、对2种的信息进行查询,并重新定义列名(查询结果可以是一个算数表达式,如出生年份可以用2021-sage表示)

select sname Name,ssex Sex,2021-sage Age from student;

 4、取消指定信息的重复行(用distinct)

select DISTINCT sno from student;

5、查询满足条件的元组

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

·比较大小

①查询计算机系全体学生名单

select sname from student where sdept='计算机系';

②查询所有年龄在20岁以下的学生姓名及其年龄

select sname,sage 
from student
where sage<20;

③查询考试成绩不及格的学生的学号

select DISTINCT sno 
from sc
where grade<60;

·确定范围

①查询年龄在20-23岁之间的学生的姓名,系别和年龄

select sname,sdept,sage 
from student
where sage between 20 and 23;

·确定集合

①查询计算机系、数学系和信息系学生的姓名和性别

select  sname,ssex
from student
where sdept in ('计算机系','数学系','信息系');

②查询 不满足①信息的学生的姓名和性别

select  sname,ssex
from student
where sdept not in ('计算机系','数学系','信息系');

·字符匹配

①查询名字中第二个字为“阳“的学生的姓名和学号(‘_'表示单字,’%‘表示多字)

select sname,sno,sex 
from student
where sname like '_欧%';

②查询以”DB_"开头,且倒数第三个字母为I的课程的详细情况(如果用户要查询的字符串本身就含有通配符%或_,这时就要使用ESCAPE '<换符字码>'短语对通配符进行转义

select *
from course
where cname like 'DB\_%i_ _'ESCAPE'\';

·设计空值的查询

①查询缺少成绩的学生的学号和相应的课程号

select sno,cno
from sc
where grade is null;

6、order by 句子

用户可以用order by 子句对调查结果按照一个或多个属性列的升序(ASC)或降序(DESC)排列,默认为升序。

①查询选了3号课程的学生的学号及其成绩,查询结果按分数的降序排序。

select sno,grade
from sc
where cno='3'
ORDER BY grade DESC;

②查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生安年龄降序排列。

select *
from student
ORDER BY sdept,sage DESC;

7、聚集函数(where子句中是不能用聚集函数作为条件表达式的,聚集函数只能用在select子句和group by中的having子句

select count(*) from student;      
select count([DISTINCT] sno) from sc; select AVG(grade) from sc where Cno='1';

 ①查询学生201215012选修课程的总学分数

select sum(credit)
from sc,course
where sno='20121501' and sc.cno=course.cno;

8、group by 子句

group by子句将查询结果按某一列或多列的值分组,值相等的为一组。

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

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

②查询平均成绩大于等于90分的学生学号和平均成绩

select sno,avg(grade)
from sc
group by sno
having avg(grade)>=90;

1.2 连接查询

1、等值与非等值连接查询

当运算条件为=时,称为等值连接。使用其他运算符成为非等值连接。连接条件中的各连接字段类型必须是可比的,但名字不必相同。

①查询每个学生及其选修课程的情况

select student.*,sc.*
from student,sc
where student.sno=sc.sno;

②查询选修2号课程且成绩在90分以上的所有学生的学号和姓名

select student.sno,sname
from student,sc
where student.sno=sc.sno and
            sc.cno='2'and sc.grade>90;

2、自身连接

①查询每一门课的间接先修课(即先修课的先修课)

select first.cno,second.cpno
from course First,course Second
where first.cpno=second.cno;

3、外连接

①查询每个学生及其选修课程的情况

select student.sno,sname,ssex,sage,sdept,cno,grade
from student LEFT JOIN sc ON(student.sno=sc.sno);

4、多表连接

①查询每个学生的学号、姓名、选修的课程名及成绩

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

1.3 嵌套查询

1、带有in谓词的子查询

①查询与“刘晨”在同一个系学习的学生

select sno,sname,sdept
from student
where sdept in
    (select sdept
        from student
        where sname='刘晨');

2、找出每个学生超过他自己选修课平均成绩的课程号

select sno,cno
from sc x
where grade>=
    (select avg(grade)
        from sc y
        where x.sno=y.sno);

3、带有any(some)或all谓词的子查询

①查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄(<>不等于,anye表示范围中的某个值,all表示范围中的所有值)

select sname,sage
from student
where sage<any
            (select sage 
            from student
            where sdept='计算机科学系')
and sdept<>'计算机科学系';

②查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄

select sname,sage
from student
where sage<all
        (select sage
        from student
        where sdept='计算机科学系')
and sdept<>'计算机科学系';

4、带有exists谓词的子查询(带有exists谓词的子查询不返回任何数据,只产生逻辑真值"true"或逻辑假值"false“

①查询没有选修了1号课程的学生姓名(若内层查询结果为非空,则外层的where子句返回真值,否则返回假值)

select sname
from student
where exists
        (select *
        from sc
        where sno=student.sno and cno='1');

 1.4 集合查询

集合操作主要包括并操作union、交操作intersect和差操作except。

①查询计算机科学系的学生及年龄不大于19岁的学生

select *
from student
where sdept='计算机科学系'
UNION
select *
from student
where sage<=19;

1.5 基于派生表的查询

子查询不仅可以出现在where子句中,还可以出现在from子句中,这时子查询生成的临时派生表成为主查询的查询对象。

①找出每个学生超过他自己选修课程平均成绩的课程号

select sno,cno
from sc,(select sno,avg(grade) from sc group by sno)
                as avg_sc(avg_sno,avg_grade);

二、数据更新

2.1查出数据

1、插入元组

①将学生张成民的信息插入到student表中

insert 
into student
values('201215126','张成民','',18,'CS');

②插入一条选课记录('201215126','1') 

关系型数据库管理系统将在新插入记录的grade列上自动地赋空值。

insert
into sc(sno,cno)
values('201215126','1');

2、插入子查询结果

①对每一个系,求学生的平均年龄,并把结果存入数据库

insert 
into dept_age(sdept,avg_age)
select sdept,avg(sage)
from student
group by sdept;

2.2 修改数据

1、修改某一个元组的值

将学生201215126的年龄改为22

update student
set sage=22
where sno='201215126';

2、修改多个元组的值

将所有学生的年龄增加1岁

update student
set sage=sage+1;

3、带子查询的修改语句

将计算机科学系全体学生的成绩置零

update sc
set grade=0
where sno in(
            select sno
            from student
            where sname='计算机科学系');

2.3 删除数据

1、删除某一元组的值

删除学号为201215127的学生记录

delete 
from student
where sno='201215127';

2、删除多个元组的值

删除所有的学生选课记录

delete
from sc;

3、带子查询的删除语句

删除计算机科学系所有学生的选课记录

delete
from sc
where sno in
            (select sno
            from student
            where sname='计算机科学系');

 

posted on 2021-08-05 22:27  阡陌祁画  阅读(613)  评论(0编辑  收藏  举报