欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL 牛刀小试 1 —— 查询操作

程序员文章站 2022-05-18 18:15:34
#创建数据库create database ST CHARACTER set utf8;#创建用户create user ST identified by '19980510';#授权用户操作该数据库grant all on ST.* to ST; #创建学生表create table Studen ......

#创建数据库
create database st character set utf8;
#创建用户
create user st identified by '19980510';
#授权用户操作该数据库
grant all on st.* to st;

----------------
#创建学生表
create table student
(
sno char(9) primary key ,
sname char(20) not null,
ssex char(2),
sage smallint,
sdept char(20)
);
#插入信息
insert into student(sno,sname,ssex,sage,sdept) values(201215121,'李勇','男',20,'cs');
insert into student(sno,sname,ssex,sage,sdept) values(201215122,'刘晨','女',19,'cs');
insert into student(sno,sname,ssex,sage,sdept) values(201215123,'王敏','女',18,'ma');
insert into student(sno,sname,ssex,sage,sdept) values(201215125,'张立','男',19,'is');
--------------
#创建课程表
create table course
(
cno char(4) primary key,
cname char(40) not null,
cpno char(4),
ccredit smallint,
foreign key(cpno) references course(cno)
);
#插入信息
insert into course(cno,cname,cpno,ccredit) values(1,'数据库',5,4);
insert into course(cno,cname,cpno,ccredit) values(2,'数学',,2);
insert into course(cno,cname,cpno,ccredit) values(3,'信息系统',1,4);
insert into course(cno,cname,cpno,ccredit) values(4,'操作系统',6,3);
insert into course(cno,cname,cpno,ccredit) values(5,'数据结构',7,4);
insert into course(cno,cname,cpno,ccredit) values(6,'数据处理',,2);
insert into course(cno,cname,cpno,ccredit) values(7,'pascal语言',6,4);
-------------------
/*
[sql]insert into course(cno,cname,cpno,ccredit) values(1,'数据库',5,4);
[err] 1452 - cannot add or update a child row: a foreign key constraint fails (`st`.`course`, constraint `course_ibfk_1` foreign key (`cpno`) references `course` (`cno`))
*/
-- 解决方法 先不录入先修课(cpno)的值
insert into course(cno,cname,ccredit) values(1,'数据库',4);
insert into course(cno,cname,ccredit) values(2,'数学',2);
insert into course(cno,cname,ccredit) values(3,'信息系统',4);
insert into course(cno,cname,ccredit) values(4,'操作系统',3);
insert into course(cno,cname,ccredit) values(5,'数据结构',4);
insert into course(cno,cname,ccredit) values(6,'数据处理',2);
insert into course(cno,cname,ccredit) values(7,'pascal语言',4);
------------------
#创建选课表
create table sc
(
sno char(9),
cno char(4),
grade smallint,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);
#插入信息
insert into sc(sno,cno,grade) values(201215121,1,92);
insert into sc(sno,cno,grade) values(201215121,2,85);
insert into sc(sno,cno,grade) values(201215121,3,88);
insert into sc(sno,cno,grade) values(201215122,2,90);
insert into sc(sno,cno,grade) values(201215122,3,80);

--------------------------
#查询全体学生的学号和姓名
select sno,sname from student ;
---------
#查询全体学生的姓名、学号、所在系
select sname,sno,sdept from student;
---------
#查询全体学生的详细记录 (查询经过计算的值)
select * from student;
-----------
#查询全体学生的姓名及其出生年份
select sname,2018-sage from student;
-----------
#查询全体学生的姓名、出生年份和所在院系,要求用小写字母表示系名
select sname,2018-sage,lower(sdept) from student;
------------
#查询选修了课程的学生学号
select sno from sc; -- select all sno from sc; -- 有重复
select distinct sno from sc; -- 无重复
------------
#查询计算机科学系全体学生的名单
select * from student where sdept = 'cs';
------------
#查询所有年龄在20岁以下的学生姓名及其年龄
select sname,sage from student where sage<20;
------------
#查询考试成绩不合格的学生的学号
select sno from sc where grade<60;
------------
#查询年龄在20——23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄
select sname,sdept,sage from student where sage>=20 and sage<=23;
select sname,sdept,sage from student where sage between 20 and 23;
------------
#查询年龄不在20——23岁之间的学生姓名、系别和年龄
select sname,sdept,sage from student where sage not between 20 and 23;
-------------
#查询计算机科学系(cs)、数学系(ma)和信息系(is)学生的姓名和性别
select sname,ssex from student where sdept in('cs','ma','is');
select sname,ssex from student where sdept='cs' or sdept='ma' or sdept='is';
-------------
#查询既不是计算机科学系、数学系、也不是信息系的学生的姓名和性别
select sname,ssex from student where sdept not in('cs','ma','is');
-------------
#查询学号为201215121的学生的详细情况
select * from student where sno like '201215121';
select * from student where sno='201215121';
-- 注意:如果like后面的匹配串中不含通配符,则可以用 = (等于)运算符取代 like 谓词,用 != 或 <> (不等于)运算符取代 not like 谓词
-------------
#查询所有姓刘的学生的姓名、学号和性别
select sname,sno,ssex from student where sname like '刘%';
-------------
#查询姓“欧阳”且全名为三个汉字的学生的姓名
select sname from student where sname like '欧阳_';
-- 注意:数据库字符集为ascii时一个汉字需要两个 _ ; 当字符集为gbk是只需要一个
--------------
#查询名字中第二个字为“阳”的学生的姓名和学号
select sname,sno from student where sname like '%阳%';
-------------
#查询所有不姓刘的学生的姓名、学号和性别
select sname,sno,ssex from student where sname not like '刘%';
-------------
#查询db_design课程的课程号和学分
select cno,ccredit from course where cname like 'db\_design' escape '\\';
-------------
#查询以“db_”开头,且倒数第三个字符为i的课程的详细情况
select * from course where cname like 'db\_%' escape '\\';
-------------
#某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号
select sno,cno from sc where grade is null; -- 注意这里的“is”不能用 = (等号)代替
-------------
#查询计算机科学系年龄在20岁以下的学生姓名
select sname from student where sdept = 'cs' and sage<20;
------------
#查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列
select sno,grade from sc where cno='3' order by grade desc; -- 降序
select sno,grade from sc where cno='3' order by grade asc; -- 升序
-------------
/*
#查询全体学生情况,查询结果按所在系的系号升序排列,同一系中学生按年龄降序排列
select * from student where order by sdept,sage desc; -- 默认为升序
[sql]select * from student where order by sdept,sage desc;
[err] 1064 - you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'order by sdept,sage desc' at line 1
*/
------------
#查询学生总人数
select count(*) from student;
------------
#查询选修了课程的学生人数
select count(distinct sno) from sc;
------------
#计算选修1号课程的学生平均成绩
select avg(grade) from sc where cno = '1';
------------
#查询选修1号课程的学生最高分数
select max(grade) from sc where cno = '1';
------------
#查询学生201215012选修课程的总学分数
select sum(ccredit) from sc,course where sno = '201215122' and sc.cno = course.cno;
------------
#求各个课程号及相应的选课人数
select cno,count(*) from sc group by cno;
-- group by 子句将查询结果按某一列或多列的值分组,值相等的为一组。
-- 分组后聚集函数将作用于每一个组,即每一组都有一个函数值
------------
#查询选修了三门以上课程的学生学号
select sno from sc group by sno having count(*)>3;
-- having 短语作用于组,从中选择满足条件的组
-------------
#查询平均成绩大于等于90分的学生学号和平均成绩
select sno, avg(grade) from sc where avg(grade)>=90 group by sno; -- 此句不正确,where子句中不能用聚集函数作为 条件表达式
select sno, avg(grade) from sc group by sno having avg(grade)>=90;
--------------------------------------------------------------- 连接查询
#查询每个学生及其选修课程的情况
select student.*,sc.* from student,sc where student.sno = sc.sno;
select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno = sc.sno; -- 自然连接实现
--------
#查询选修2号课程且成绩在90分以上的所有学生的学号和姓名
select student.sno,sname from sc,student where student.sno = sc.sno and sc.cno='2' and sc.grade >=90;
select student.sno,sname from student inner join sc on(student.sno = sc.sno) where cno= '2' and grade >=90;
----------
#查询每一门课的间接先修课(即先修课的先修课).按照cno降序排列
select first.cno,second.cpno from course as first,course as second where first.cpno = second.cno and second.cpno is not null order by first.cno desc -- first,second 是course表的两个别名
-----------
#查询全体学生的详细信息和所选课程号及成绩
select student.sno,sname,ssex,sage,sdept,cno,grade from student left outer join sc on (student.sno = sc.sno);
select student.*,cno,grade from student left outer join sc on (student.sno = sc.sno);
-----------



#查询每个学生的学号、姓名、选修的课程名及成绩
select student.sno,student.sname,course.cname,sc.grade from student,course,sc where student.sno = sc.sno and course.cno = sc.cno;
--------------------------------------------------------------- 嵌套查询
#查询与“刘晨”在同一个系学习的学生
select student.* from student where sdept in (select sdept from student where sname = '刘晨');
-----------
#查询选修了课程名为“信息系统”的学生学号和姓名
select sno,sname from student where sno in (select sno from sc where cno in (select cno from course where cname = '信息系统'));
select student.sno,sname from student,course,sc where course.cname = '信息系统' and student.sno = sc.sno and course.cno = sc.cno;
----------
#找出每个学生超过他自己选修课程平均成绩的课程号
select sno,cno from sc x where grade >= (select avg(grade) from sc y where y.sno = x.sno);
-----------
#查询非计算机科学系中比计算机科学系任意一个学生年龄小于的学生姓名和年龄
select sname,sage from student where sage<any(select sage from student where sdept = 'cs')and sdept<>'cs';
select sname,sage from student where sage<(select max(sage) from student where sdept = 'cs')and sdept<>'cs';
-----------
#查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄
select sname,sage from student where sage<all(select sage from student where sdept = 'cs')and sdept<>'cs';
select sname,sage from student where sage<(select min(sage) from student where sdept = 'cs')and sdept<>'cs';
-----------
#查询所有选修了1号课程的学生姓名
select sname from student where exists (select * from sc where sno = student.sno and cno = '1');
select sname,sno from student where sno in (select sno from sc where cno = '1');
select sname,student.sno,grade from student,sc where  sc.cno = '1' and student.sno = sc.sno;
------------
#查询没有选修1号课程的学生姓名
select sno,sname from student where not exists (select * from sc where sno = student.sno and cno = '1');
------------
#查询选修了全部课程的学生姓名

-----------
#查询至少选修了学生201215122选修的全部课程的学生号码


---------------------------------------------------------------- 集合查询
-- 注意:参加集合操作的各查询结果的列数必须相同;对应项的数据类型也必须相同
#查询计算机科学系的学生及年龄不大于19岁的学生

------------
#查询选修了课程1或者选修了课程2的学生

------------

#查询计算机科学系的学生与年龄不大于19岁的学生的交集


-------------
#查询既选修了课程1又选修了课2的学生

-------------
#查询计算机科学系的学生与年龄不大于19岁的学生的差集
2018-10-07 15:57:06