第九周課堂作業 包括建表 進行各種查詢

一共有六張表格,接下來根據要求進行建表與查詢

關於六張表的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 ‘^趙…江$’;

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