🏝️专栏:Mysql_猫咪-9527的博客-CSDN博客
🌅主页:猫咪-9527-CSDN博客“欲穷千里目,更上一层楼。会当凌绝顶,一览众山小。”
目录
实验二单表查询
1.实验数据如下
student 表(学生表)
course 表(课程表)
teacher 表(教师表)
score 表(成绩表)
2. 插入数据
student 表中的数据
course 表中的数据
teacher 表中的数据
score 表中的数据
2-1 简单查询语句
2-2 分组统计
实验二单表查询
实验目的:
通过实验掌握数据库系统单表查询的方法
1.实验数据如下
student 表(学生表)
CREATE TABLE student (sno CHAR(5) PRIMARY KEY,snme VARCHAR(20) NOT NULL, sdept VARCHAR(20) NOT NULL, sclass CHAR(2) NOT NULL, ssex CHAR(1), birthday DATE, totalcredit DECIMAL(4,1)
);
course 表(课程表)
CREATE TABLE course (cno CHAR(3) PRIMARY KEY,cname VARCHAR(50), ctime DECIMAL(3,0), credit DECIMAL(3,1)
);
teacher 表(教师表)
CREATE TABLE teacher (tno CHAR(6) PRIMARY KEY, tname VARCHAR(20), tsex CHAR(1), tdept VARCHAR(20)
);
score 表(成绩表)
CREATE TABLE score (sno CHAR(5), cno CHAR(3), tno CHAR(6), grade DECIMAL(5,1), PRIMARY KEY (sno, cno, tno), CONSTRAINT fk_sno FOREIGN KEY(sno) REFERENCES student(sno),CONSTRAINT fk_cno FOREIGN KEY(cno) REFERENCES course(cno),CONSTRAINT fk_tno FOREIGN KEY(tno) REFERENCES teacher(tno)
);
2. 插入数据
student 表中的数据
INSERT INTO student VALUES('96001', '马小燕', '计算机', '01', '女', '2000/01/02', 0);
INSERT INTO student VALUES('96002', '黎明', '计算机', '01', '男', '2000/03/05', 0);
INSERT INTO student VALUES('96003', '刘东明', '数学', '01', '男', '2000/10/05', 0);
INSERT INTO student VALUES('96004', '赵志勇', '信息', '02', '男', '2000/08/08', 0);
INSERT INTO student VALUES('97001', '马蓉', '数学', '02', '女', '2001/03/04', 0);
INSERT INTO student VALUES('97002', '李成功', '计算机', '01', '男', '2001/09/10', 0);
INSERT INTO student VALUES('97003', '黎明', '信息', '03', '女', '2002/02/08', 0);
INSERT INTO student VALUES('97004', '李丽', '计算机', '02', '女', '2002/01/05', 0);
INSERT INTO student VALUES('96005', '司马志明', '计算机', '02', '男', '2001/11/23', 0);
course 表中的数据
INSERT INTO course VALUES('001', '数学分析', 64, 4);
INSERT INTO course VALUES('002', '普通物理', 64, 4);
INSERT INTO course VALUES('003', '微机原理', 56, 3.5);
INSERT INTO course VALUES('004', '数据结构', 64, 4);
INSERT INTO course VALUES('005', '操作系统', 56, 3.5);
INSERT INTO course VALUES('006', '数据库原理', 56, 3.5);
INSERT INTO course VALUES('007', '编译原理', 48, 3);
INSERT INTO course VALUES('008', '程序设计', 32, 2);
teacher 表中的数据
INSERT INTO teacher VALUES('052501', '王成刚', '男', '计算机');
INSERT INTO teacher VALUES('052502', '李正科', '男', '计算机');
INSERT INTO teacher VALUES('052503', '严敏', '女', '数学');
INSERT INTO teacher VALUES('052504', '赵高', '男', '数学');
INSERT INTO teacher VALUES('052505', '刘玉兰', '女', '计算机');
INSERT INTO teacher VALUES('052506', '王成刚', '男', '信息');
INSERT INTO teacher VALUES('052507', '马悦', '女', '计算机');
score 表中的数据
INSERT INTO score VALUES('96001', '001', '052503', 77.5);
INSERT INTO score VALUES('96001', '003', '052501', 89);
INSERT INTO score VALUES('96001', '004', '052502', 86);
INSERT INTO score VALUES('96001', '005', '052505', 82);
INSERT INTO score VALUES('96002', '001', '052504', 88);
INSERT INTO score VALUES('96002', '003', '052502', 92.5);
INSERT INTO score VALUES('96002', '006', '052507', 90);
INSERT INTO score VALUES('96005', '004', '052502', 92);
INSERT INTO score VALUES('96005', '005', '052505', 90);
INSERT INTO score VALUES('96005', '006', '052505', 89);
INSERT INTO score VALUES('96005', '007', '052507', 78);
INSERT INTO score VALUES('96003', '001', '052504', 69);
INSERT INTO score VALUES('97001', '001', '052504', 96);
INSERT INTO score VALUES('97001', '008', '052505', 95);
INSERT INTO score VALUES('96004', '001', '052503', 87);
INSERT INTO score VALUES('96003', '003', '052501', 91);
INSERT INTO score VALUES('97002', '003', '052502', 91);
INSERT INTO score VALUES('97002', '004', '052505', NULL);
INSERT INTO score VALUES('97002', '006', '052507', 92);
INSERT INTO score VALUES('97004', '005', '052502', 90);
INSERT INTO score VALUES('97004', '006', '052501', 85);
注:把上面的实验数据添加上再开始实验。
2-1 简单查询语句
- 查询年龄大于20的学生的学号、姓名和年龄,结果列别名为“学号”“姓名”“年龄”
select sno 学号, snme 姓名, datediff(now(),birthday)/365 年龄
from student where date_add(birthday,interval 20 year)<now();
- 查询选修了课程的学生学号,结果表中学号显示唯一
select distinct sno from score;
- 查询不是计算机系或信息系的学生
select *from student where sdept not in ('计算机','信息');
- 查询学时在1~50的课程信息
select *from course where ctime between 1 and 50;
查询姓名长度至少是3个汉字且倒数第三个汉字必须是“马”的学生
select *from student where snme like '%马__';
- 查询选修老师编号为052501、成绩在80~90、学号为96xxx的学生成绩
select *from score where sno like '96%'
and tno ='052501' and grade between 80 and 90;
- 查询没有成绩的学生的学号和课程号
select *from score where grade is null;
2-2 分组统计
- 查询学生总人数
select count(*) from student;
- 查询选修了课程的学生人数
SELECT COUNT(DISTINCT 学号) AS 选修课程学生人数
FROM 选修;
- 查询选修各门课程的最高、最低与平均成绩
select cno,max(grade),min(grade),avg(grade) from score group by cno;
- 查询学生人数不足3人的系别及其相应的学生人数
select sdept 系,count(*) from student group by sdept having count(*)<3 ;
- 查询各系中各班的学生人数,结果按班级人数降序排列
select sdept,sclass,count(*) from student group by sdept,
sclass order by count(*) desc;