第九周课堂作业 包括建表 进行各种查询

一共有六张表格,接下来根据要求进行建表与查询

关于六张表的txt存储的数据
链接:https://pan.baidu.com/s/1Xl2WaW0S9SUdcsNfCGj2Hg
提取码:y1l3
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

建表基本操作

1、使用show databases语句查看MySQL服务器中的所有数据库。

show databases;

2、 创建教务管理数据库teaching,并指定字符集为gb2312,校对原gb2312_chinese_ci。

create database teaching character set gb2312 collate gb2312_chinese_ci;

3、显示数据库teaching的结构信息。

show create database teachings;

4.创建student的表结构以及将对应txt内容加载到数据库中

create table if not exists student
(
studentno char(11) comment ‘学生学号’,
sname char(8) not null comment ‘学生姓名’,
sex enum(‘男’,‘女’) not null comment ‘性别’,
birthdate date not null comment ‘出生日期’,
entrance int(3) not null comment ‘入学成绩’,
phone Varchar(12) not null comment ‘电话’,
Email Varchar(20) not null comment ‘电子邮箱’,
primary key(studentno)
);
注意:题目中enum(2) 在实际里写enum(‘男’,‘女’)

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\student.txt” into table student;

5.创建course表,并且载入数据

create table if not exists course
(
courseno char(6) comment ‘课程编号’,
cname char(20) not null comment ‘课程名称’,
type char(8) not null comment ‘类别’,
period int(2) not null comment ‘总学时’,
exp int(2) not null comment ‘实验学时’,
term int(2) not null comment ‘开课学期’,
primary key(courseno)
);

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\course.txt” into table course;

6.建立score表,载入数据

create table if not exists score
-> (
-> studentno char(11) not null comment ‘学号’,
-> courseno char(6) not null comment ‘课程编号’,
-> daily float(3,1) not null comment ‘平时成绩’,
-> final float(3,1) not null comment ‘期末成绩’,
-> primary key(studentno,courseno)
-> );

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\score.txt” into table score;

7.建立teacher表,载入数据

create table if not exists teacher
-> (
-> teacherno char(6) not null comment ‘教师编号’,
-> tname char(8) not null comment ‘教师姓名’,
-> major char(10) not null comment ‘专业’,
-> prof char(10) comment ‘职称’,
-> department char(16) not null comment ‘院系部门’,
-> primary key(teacherno)
-> );

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\teacher.txt” into table teacher;

8.建立teach_course表,载入数据

create table if not exists teach_course
-> (
-> teacherno char(6) not null comment ‘教师编号’,
-> courseno char(6) not null comment ‘课程编号’,
-> primary key(teacherno,courseno)
-> );

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\teach_course.txt” into table teach_course;

9.建立sc表,载入数据

create table if not exists sc
-> (
-> sc_no int(6) not null comment ‘选课号’,
-> studentno char(11) not null comment ‘学号’,
-> courseno char(6) not null comment ‘课程编号’,
-> teacherno char(6) not null comment ‘教师工号’,
-> sc_time timestamp not null comment ‘选课时间’,
-> primary key(sc_no)
-> );

load data local infile “G:\大一下学期\数据仓库构建与应用\第七周 2020.4.21\txt文件\sc.txt” into table sc;

10.将表sc重命名为sc_course。

alter table sc rename to sc_course;

11.在student表的Email列后面增加一列address。

alter table student add address varchar(30) not null after Email;

12.删除student表的字段address。

alter table student drop address;

13.删除student表中名字为许东山的记录

delete from student where sname=‘许东山’;

14.插入许东山对应记录

insert into student values(‘18122210009’,‘许东山’,‘男’,‘1999/11/5’,789,‘13623456788’,‘[email protected]’);

15.插入多条记录 (replace与insert意思一样)

replace into student values(‘18122210008’,‘徐东山’,‘男’,‘1999/11/5’,789,‘13623456788’,‘[email protected]’),(‘1812221007’,‘网费’,‘女’,‘1972/12/12’,790,‘13972456091’,‘[email protected]’);

16.删除student表中的数据 再从txt载入文件数据

delete from student; 或者truncate student

数据表查询:

1.在student表中显示所有姓何或姓韩的学生的姓名、生日和Email。
mysql> select sname as 姓名,birthdate as 生日,Email

select sname,birthdate,Email
-> from student
-> where sname like ‘何%’ or sname like ‘韩%’;
注意:这里的%可以表示多个字符

2.查询计算机学院的具有高级职称教师的教师号、姓名和从事专业。

select teacherno,tname,major
-> from teacher
-> where prof in(‘教授’,‘副教授’); //这里根据实际来,有限范围内

3.在student表中查询高于850分的学生学号、姓名和入学成绩,并按照入学成绩的降序排列。

select studentno,sname,entrance
-> from student
-> where entrance>850
-> order by entrance desc; //有desc的是降序

4.在score表中查询总评成绩大于90分的学生的学号、课程号和总评成绩,并先按照课程号的升序、再按照总评成绩的降序排列。总评成绩计算公式如下:
总评成绩=daily0.3+final0.7

select studentno,courseno,(daily0.3+final0.7) 总评
-> from score
-> where (daily0.3+final0.7)>90
-> order by courseno,(daily0.3+final0.7) desc;

5.利用group by子句对score表数据分组,显示每个学生的学号和平均总评成绩。总评成绩计算公式如下:

select studentno,round(avg(daily0.3+final0.7),2) as 平均总评
-> from score
-> group by studentno; //根据学生学号分组

6.使用group by关键字和group_concat()函数对score表中的studentno字段进行分组查询。可以查看选学该门课程的学生学号。

select courseno 课程号,group_concat(studentno) 选课学号 //分别取别名
-> from score
-> group by courseno; //对课程号进行分组

7.查询选课在3门以上且各门课程期末成绩均高于75分的学生的学号及其总成绩,查询结果按总成绩降序列出。

select studentno,sum(daily0.3+final0.7)as 总成绩
-> from score
-> where final>75
-> group by studentno
-> having count(courseno)>=3 //分组后筛选选课数
-> order by sum(daily0.3+final0.7) desc; //根据降序排序

8.查询student表的学号、姓名、出生日期和电话,按照entrance进行降序排列,显示前3条记录。

select studentno,sname,birthdate,phone
-> from student
-> order by entrance desc
-> limit 3;

9、查询score表中,期末成绩final高于85分的,按照平时成绩daily进行升序排列,从编号2开始,查询5条记录。

select *
-> from score
-> where final>85
-> order by daily
-> limit 2,5;

10.查询score表中学生的期末总成绩大于270分的学生学号、总成绩及平均成绩。

select studentno,sum(final) 总成绩,avg(final) 平均成绩
-> from score
-> group by studentno
-> having sum(final)>270;

11.查询选修课程号为c05109号课程的期末最高分、最低分及之间相差的分数

select max(final) 最高分,min(final) 最低分,max(final)-min(final) 极值
-> from score
-> where courseno=‘c05109’;

12.利用右外连接方式查询教师的排课情况

select a.teacherno,tname,major,courseno
-> from teacher a right join teach_course
-> on a.teacherno = teach_course.teacherno;

13.查询18级学生的学号、姓名、课程名、期末成绩及学分。

连接查询
这里三张表分别为student,course,score
其中studentno为student与score有相同名称所以要有别名 用来区分
最后substring来分割前学号前两位为18

select a.studentno,sname,cname,final,round(period/16,0) 学分
-> from student a join score b on a.studentno=b.studentno
-> join course c on b.courseno=c.courseno
-> where substring(a.studentno,1,2) = ‘18’;

14、查询期末成绩比选修该课程平均期末成绩低的学生的学号、课程号和期末成绩。

select studentno,courseno,final
-> from score as a
-> where final<(select avg(final) as 本课程平均成绩
-> from score as b
-> where a.courseno=b.courseno
-> group by courseno);’

注意:(1) 这是自连接后面的部分 获取对应课程的平均成绩及课程号
select courseno,round(avg(final),2) as 课程平均成绩
-> from score
-> group by courseno;

15.查找score表中所有比c05109课程期末成绩都高的学号、姓名、电话和期末成绩。
两张表:student与score

select a.studentno,sname,phone,final
-> from student a join score b
-> on a.studentno=b.studentno
-> where final>all
-> (select final from score where courseno=‘c05109’); //这里求得所有c05109的期末成绩

16、将student表中入学成绩低于800的所有学生的期末成绩增加5%。

出现错误:Out of range value for column ‘final’ at row 5
解决:是在score表汇总的final数据类型范围不够在增加5%之后
在这里插入图片描述

修改数据类型:alter table score modify final float(6,1);

update score
-> set final=final*1.05
-> where studentno in
-> (select studentno from student where entrance<800);

17、查询student表中姓“赵”的学生的部分信息。

select studentno,sname,birthdate,phone
-> from student
-> where sname regexp ‘^赵’;

18.查询student表中学生电话号码尾数为5的学生部分信息。

select studentno,sname,birthdate,phone
-> from student
-> where phone regexp ‘5$’;

19、查询学生电话号码出现131或132数字的学生信息。

select studentno,sname,phone,Email
-> from student
-> where phone regexp ‘131|132’;

20、要实现查询学生姓名sname字段中以“赵”开头,以“江”结束的,中间包含两个字符的学生信息,可以通过正则表达式查询来实现,其中正则表达式中,^表示字符串的开始位置,$表示字符串的结束位置,.表示除“\n”以外的任何单个字符(此例中汉字按2个字符计算)。

select studentno,sname,phone,birthdate
-> from student
-> where sname regexp ‘^赵…江$’;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章