mysql实训

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 2018年7月11日笔记1.新建数据库设有一个数据库,包括四个表:学生表(student)、课程表(course)、成绩表(score)以及教师信息表(teacher)。

2018年7月11日笔记

1.新建数据库

设有一个数据库,包括四个表:学生表(student)、课程表(course)、成绩表(score)以及教师信息表(teacher)。用SQL语句创建四个表并完成相关题目。
创建学生表

create table student(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
) engine = innodb default charset = utf8;

创建教师表

create table teacher(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20),
depart varchar(20) not null
)engine = innodb default charset = utf8;

创建课程表

create table course(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
) engine = innodb default charset = utf8;

创建成绩表

create table score(
sno varchar(20) not null,
foreign key(sno) references student(sno),
cno varchar(20) not null,
foreign key(cno) references course(cno)
degree decimal,
) engine = innodb default charset = utf8;

2.插入数据库

插入学生表数据

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);

插入教师表数据

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');

插入课程表数据

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,831);

插入成绩表数据

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'9-888',79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);

3.查询数据

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

select sname,ssex,class from student;

2、 查询教师所有的单位即不重复的Depart列。

select depart from teacher group by depart;

3、 查询Student表的所有记录。

select * from student;

4、 查询Score表中成绩在60到80之间的所有记录。(between)

select * from score where degree between 60 and 80;

5、 查询Score表中成绩为85,86或88的记录。(in)

select * from score where degree in (85,86,88);

6、 查询Student表中“95031”班或性别为“女”的同学记录。

select * from student where class = '95031' or ssex = '女';

7、 以Class降序查询Student表的所有记录。

select * from student order by class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。

select * from score order by cno asc,degree desc;

9、 查询“95031”班的学生人数。

select count(*) from student where class = '95031';

10、查询Score表中的最高分的学生学号和课程号。(排序)

select sno,cno from score order by degree desc;

11、查询每门课的平均成绩。(group by)

select cname,avg(degree) from score inner join course where course.cno = score.cno group by course.cname;

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。(group by having)

select cno,avg(degree) from score where cno like "3%" group by cno having count(sno) >= 5;

13、查询最低分大于70,最高分小于90的Sno列。

select sno from score group by sno having max(degree) < 90 and min(degree) > 70 ;

14、查询所有学生的Sname、Cno和Degree列。

select sname,score.cno,degree from student inner join score where student.sno = score.sno;

15、查询所有学生的Sno、Cname和Degree列。

select student.sno,sname,degree from student inner join score where student.sno = score.sno;

16、查询所有学生的Sname、Cname和Degree列。

select sname,cname,degree from student inner join score,course on student.sno = score.sno and course.cno = score.cno;

17、查询“95033”班学生的平均分。(选)

select avg(degree) from student inner join score where class = '95033' and student.sno = score.sno;

18、假设使用如下命令建立了一个grade表,查询所有同学的Sno、Cno和rank列。

create table grade(low int(3),high int(3),rank varchar(20));
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
commit;

select sno,cno,rank from score inner join grade where degree < high and degree >low;

19、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。(选)

select * from score where degree > (select degree from score where sno = '109' and cno ='3-105');

20、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。(选)

select sno,sname,sbirthday
from student,(select year(sbirthday) as birthyear from student where sno = 108) as s1
where year(sbirthday) = s1.birthyear;

21、查询“张旭“教师任课的学生成绩。(选)

select tname,cname,sname,degree
from teacher,course,student,score
where teacher.tno = course.tno and course.cno = score.cno and score.sno = student.sno and teacher.tname = '张旭';

22、查询95033班和95031班全体学生的记录。(in)

select * from student where class in ('95033','95031');

23、查询存在有85分以上成绩的课程Cno.

select cno from score group by cno having max(degree) >= 85;

24、查询出“计算机系“教师所教课程的成绩表。(选)

select score.sno,sname,degree
from teacher,score,course,student
where teacher.tno = course.tno and course.cno = score.cno and score.sno = student.sno and teacher.depart = '计算机系';

25、查询成绩比该课程平均成绩低的同学的成绩表。(选)

select s.sno,s.cno,s.degree
from score as s,(select cno,avg(degree) as avgd from score group by cno ) as avgs
where s.cno = avgs.cno and s.degree < avgs.avgd;

26、查询所有任课教师的Tname和Depart.(选)

select tname,depart from teacher;

27、查询至少有2名男生的班号。(group by having)

select class,count(name) from student group by class having count(name) >= 2;

28、查询Student表中不姓“王”的同学记录。(not like)

select * from student where sname not like "王%";

29、查询Student表中每个学生的姓名和年龄。

select sname,2018 - year(sbirthday) as age from student;

30、查询Student表中最大和最小的Sbirthday日期值。

select max(sbirthday),min(sbirthday) from student;

31、以班号和年龄从大到小的顺序查询Student表中的全部记录。
下面两种SQL语句一样的效果,年龄大则出生日期小

select * from student order by class desc,sbirthday;
select * from student order by class desc,sbirthday asc;

32、查询“男”教师及其所上的课程。

select tname,cname from teacher,course where teacher.tno = course.tno and tsex = '男';

33、查询每一门科目最高分同学的Sno、Cno和Degree列。(选)

select sno,cno,max(degree) from score group by cno ;

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
SQL 前端开发 Java
Java+Mysql图书管理系统(完整实训代码)
​ ✨博主:命运之光 🌸专栏:Python星辰秘典 🐳专栏:web开发(html css js) ❤️专栏:Java经典程序设计 ☀️博主的其他文章:点击进入博主的主页
671 0
|
SQL 关系型数据库 MySQL
MySQL数据表的结构的同步练习与实训
进行MySQL数据表的结构的同步练习与实训。
224 0
|
关系型数据库 MySQL 数据库
又一个Mysql在线实训平台,没有安装,在线即可学习
又一个Mysql在线实训平台,没有安装,在线即可学习
又一个Mysql在线实训平台,没有安装,在线即可学习
|
SQL 关系型数据库 MySQL
一份热乎乎的MySQL实训真题,让你成为SQL高手
一份热乎乎的MySQL实训真题,让你成为SQL高手
147 0
一份热乎乎的MySQL实训真题,让你成为SQL高手
|
3天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
47 25
|
12天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
91 42
|
26天前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
30天前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
257 0
|
2月前
|
存储 Oracle 关系型数据库
数据库传奇:MySQL创世之父的两千金My、Maria
《数据库传奇:MySQL创世之父的两千金My、Maria》介绍了MySQL的发展历程及其分支MariaDB。MySQL由Michael Widenius等人于1994年创建,现归Oracle所有,广泛应用于阿里巴巴、腾讯等企业。2009年,Widenius因担心Oracle收购影响MySQL的开源性,创建了MariaDB,提供额外功能和改进。维基百科、Google等已逐步替换为MariaDB,以确保更好的性能和社区支持。掌握MariaDB作为备用方案,对未来发展至关重要。
73 3
|
2月前
|
SQL 关系型数据库 MySQL
数据库灾难应对:MySQL误删除数据的救赎之道,技巧get起来!之binlog
《数据库灾难应对:MySQL误删除数据的救赎之道,技巧get起来!之binlog》介绍了如何利用MySQL的二进制日志(Binlog)恢复误删除的数据。主要内容包括: 1. **启用二进制日志**:在`my.cnf`中配置`log-bin`并重启MySQL服务。 2. **查看二进制日志文件**:使用`SHOW VARIABLES LIKE &#39;log_%&#39;;`和`SHOW MASTER STATUS;`命令获取当前日志文件及位置。 3. **创建数据备份**:确保在恢复前已有备份,以防意外。 4. **导出二进制日志为SQL语句**:使用`mysqlbinlog`
117 2