目录
1 建立表并插入数据
1.1 SQL语句
1.2 截图
2 单表查询
2.1 选择表中若干列
2.1.1 SQL语句
2.1.2 截图
2.2 选择表中若干元组
2.2.1 SQL语句
2.2.2 截图
2.3 order by子句
2.3.1 SQL语句
2.3.2 截图
2.4 聚集函数
2.4.1 SQL语句
2.4.2 截图
2.5 group by子句
2.5.1 SQL语句
2.5.2 截图
3 连接查询
3.1 等值连接
3.1.1 SQL语句
3.1.2 截图
3.2 自身连接
3.2.1 SQL语句
3.2.2 截图
3.3 外连接
3.3.1 SQL语句
3.3.2 截图
3.4 多表连接
3.4.1 SQL语句
3.4.2 截图
4 嵌套查询
4.1 in的子查询
4.1.1 SQL语句
4.1.2 截图
4.2 比较运算符的子查询
4.2.1 SQL语句
4.2.2 截图
4.3 All或ANY的子查询
4.3.1 SQL语句
4.3.2 截图
4.4 Exist的子查询
4.4.1 SQL语句
4.4.2 截图
5 集合查询
5.1 Union
5.1.1 SQL语句
5.1.2 截图
5.2 Intersect
5.2.1 SQL语句
5.2.2 截图
5.3 Except
5.3.1 SQL语句
5.3.2 截图
1 建立表并插入数据
1.1 SQL语句
create database ExperimentThree;
use ExperimentThree;
create table Student3
(
Sno char(2) not null,
Sname varchar(20) not null,
Ssex char(2) not null,
constraint s2 check(Ssex in('男', '女')),
Sage int not null,
constraint s3 check (Sage between 0 and 130),
Department varchar(20) not null
);
create table Course3
(
Cno char(2) not null ,
Cname varchar(20) not null,
Cprepare varchar(20) not null,
Ccredit float not null,
constraint c3 check(0 < Ccredit)
);
create table SC3
(
Sno char(2) not null,
Cno char(2) not null,
Performance float not null,
constraint sc1 check(Performance between 0 and 100)
);
insert into student3(sno, sname, ssex, sage, department)
values (21, '张三', '男', 18, '计通学院'),
(04, '李四', '男', 19, '化学院'),
(02, '翠花', '女', 18, '文新学院');
insert into course3(cno, cname, cprepare, ccredit)
values (11, '概率论', '高等数学', 3),
(12, '数字电路', '离散结构', 2.5),
(13, '数据结构', '离散结构', 3.5),
(01, '嵌入式', '数字电路', 2);
insert into sc3(sno, cno, performance)
values (21, 11, 66),
(02, 01, 89),
(04, 12, 59);
1.2 截图
2 单表查询
2.1 选择表中若干列
2.1.1 SQL语句
use experimentThree;
select Sname, Department
from student3;
2.1.2 截图
2.2 选择表中若干元组
2.2.1 SQL语句
use experimentThree;
select Sno
from student3;
2.2.2 截图
2.3 order by子句
2.3.1 SQL语句
use experimentThree;
select Sno, Performance
from sc3
where Performance > 60
order by Performance desc;
2.3.2 截图
2.4 聚集函数
2.4.1 SQL语句
use experimentThree;
select count(Cno)
from course3
where Ccredit >= 3;
2.4.2 截图
2.5 group by子句
2.5.1 SQL语句
use experimentThree;
select Sno
from sc3
group by Sno
having avg(Performance) >= 60;
2.5.2 截图
3 连接查询
3.1 等值连接
3.1.1 SQL语句
use experimentThree;
select *
from student3, sc3
where student3.Sno = sc3.Sno;
3.1.2 截图
3.2 自身连接
3.2.1 SQL语句
use experimentThree;
select first.cname, second.cprepare
from course3 first, course3 second
where first.Cprepare = second.Cname;
3.2.2 截图
3.3 外连接
3.3.1 SQL语句
use experimentThree;
select course3.*, sc3.*
from course3 left outer join sc3 on course3.Cno = sc3.Cno;
3.3.2 截图
3.4 多表连接
3.4.1 SQL语句
use experimentThree;
select student3.sno, Sname, Cname, Performance
from student3,sc3,course3
where student3.Sno = sc3.Sno and sc3.Cno = course3.Cno;
3.4.2 截图
4 嵌套查询
4.1 in的子查询
4.1.1 SQL语句
use experimentThree;
select Cno, Cname
from course3
where Cno in (
select Cno
from sc3
);
4.1.2 截图
4.2 比较运算符的子查询
4.2.1 SQL语句
use experimentThree;
select Cno, Cname
from course3
where Cno = (
select Cno
from sc3
where Performance < 60
);
4.2.2 截图
4.3 All或ANY的子查询
4.3.1 SQL语句
use experimentThree;
select Sname, Sage
from student3
where Sage <= any (
select Sage from student3
where Sage = 18
);
4.3.2 截图
4.4 Exist的子查询
4.4.1 SQL语句
use experimentThree;
select Sno, Sname
from student3
where exists(
select *
from sc3
where Performance >= 60 and sc3.Sno = student3.Sno
);
4.4.2 截图
5 集合查询
5.1 Union
5.1.1 SQL语句
use experimentThree;
select *
from student3
where Department = '计通学院'
union
select *
from student3
where Sage < 19;
5.1.2 截图
5.2 Intersect
5.2.1 SQL语句
use experimentThree;
select *
from student3
where Department = '计通学院'
intersect
select *
from student3
where Sage < 19;
5.2.2 截图
5.3 Except
5.3.1 SQL语句
use experimentThree;
select *
from student3
where Department = '化学院'
except
select *
from student3
where Sage < 19;