2
2
drop database if exists school;
3
3
4
4
-- 创建名为school的数据库并设置默认的字符集和排序方式
5
- create database school default charset utf8 collate utf8_bin ;
5
+ create database school default charset utf8;
6
6
7
7
-- 切换到school数据库上下文环境
8
8
use school;
9
9
10
10
-- 创建学院表
11
11
create table tb_college
12
12
(
13
- collid int not null auto_increment comment ' 编号' ,
14
- collname varchar (50 ) not null comment ' 名称' ,
15
- collmaster varchar (20 ) not null comment ' 院长' ,
16
- collweb varchar (511 ) default ' ' comment ' 网站' ,
13
+ collid int auto_increment comment ' 编号' ,
14
+ collname varchar (50 ) not null comment ' 名称' ,
15
+ collintro varchar (500 ) default ' ' comment ' 介绍' ,
17
16
primary key (collid)
18
17
);
19
18
20
19
-- 创建学生表
21
20
create table tb_student
22
21
(
23
- stuid int not null comment ' 学号' ,
24
- stuname varchar (20 ) not null comment ' 姓名' ,
25
- stusex bit default 1 comment ' 性别' ,
26
- stubirth date not null comment ' 出生日期' ,
27
- stuaddr varchar (255 ) default ' ' comment ' 籍贯' ,
28
- collid int not null comment ' 所属学院' ,
22
+ stuid int not null comment ' 学号' ,
23
+ stuname varchar (20 ) not null comment ' 姓名' ,
24
+ stusex boolean default 1 comment ' 性别' ,
25
+ stubirth date not null comment ' 出生日期' ,
26
+ stuaddr varchar (255 ) default ' ' comment ' 籍贯' ,
27
+ collid int not null comment ' 所属学院' ,
29
28
primary key (stuid),
30
29
foreign key (collid) references tb_college (collid)
31
30
);
32
31
33
- -- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid);
34
-
35
32
-- 创建教师表
36
33
create table tb_teacher
37
34
(
38
- teaid int not null comment ' 工号' ,
39
- teaname varchar (20 ) not null comment ' 姓名' ,
40
- teatitle varchar (10 ) default ' 助教' comment ' 职称' ,
41
- collid int not null comment ' 所属学院' ,
35
+ teaid int not null comment ' 工号' ,
36
+ teaname varchar (20 ) not null comment ' 姓名' ,
37
+ teatitle varchar (10 ) default ' 助教' comment ' 职称' ,
38
+ collid int not null comment ' 所属学院' ,
42
39
primary key (teaid),
43
40
foreign key (collid) references tb_college (collid)
44
41
);
45
42
46
43
-- 创建课程表
47
44
create table tb_course
48
45
(
49
- couid int not null comment ' 编号' ,
50
- couname varchar (50 ) not null comment ' 名称' ,
51
- coucredit int not null comment ' 学分' ,
52
- teaid int not null comment ' 授课老师' ,
46
+ couid int not null comment ' 编号' ,
47
+ couname varchar (50 ) not null comment ' 名称' ,
48
+ coucredit int not null comment ' 学分' ,
49
+ teaid int not null comment ' 授课老师' ,
53
50
primary key (couid),
54
51
foreign key (teaid) references tb_teacher (teaid)
55
52
);
56
53
57
54
-- 创建选课记录表
58
- create table tb_score
55
+ create table tb_record
59
56
(
60
- scid int auto_increment comment ' 选课记录编号' ,
61
- stuid int not null comment ' 选课学生' ,
62
- couid int not null comment ' 所选课程' ,
63
- scdate datetime comment ' 选课时间日期' ,
64
- scmark decimal (4 ,1 ) comment ' 考试成绩' ,
65
- primary key (scid),
66
- foreign key (stuid) references tb_student (stuid),
67
- foreign key (couid) references tb_course (couid)
57
+ recid int auto_increment comment ' 选课记录编号' ,
58
+ sid int not null comment ' 选课学生' ,
59
+ cid int not null comment ' 所选课程' ,
60
+ seldate datetime default now() comment ' 选课时间日期' ,
61
+ score decimal (4 ,1 ) comment ' 考试成绩' ,
62
+ primary key (recid),
63
+ foreign key (sid) references tb_student (stuid),
64
+ foreign key (cid) references tb_course (couid),
65
+ unique (sid, cid)
68
66
);
69
67
70
- -- 添加唯一性约束(一个学生选某个课程只能选一次)
71
- alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid);
72
-
73
68
-- 插入学院数据
74
- insert into tb_college (collname, collmaster, collweb ) values
75
- (' 计算机学院' , ' 左冷禅 ' , ' http://www.abc.com ' ),
76
- (' 外国语学院' , ' 岳不群 ' , ' http://www.xyz.com ' ),
77
- (' 经济管理学院' , ' 风清扬 ' , ' http://www.foo.com ' );
69
+ insert into tb_college (collname, collintro ) values
70
+ (' 计算机学院' , ' 计算机学院1958年设立计算机专业,1981年建立计算机科学系,1998年设立计算机学院,2005年5月,为了进一步整合教学和科研资源,学校决定,计算机学院和软件学院行政班子合并统一运作、实行教学和学生管理独立运行的模式。 学院下设三个系:计算机科学与技术系、物联网工程系、计算金融系;两个研究所:图象图形研究所、网络空间安全研究院(2015年成立);三个教学实验中心:计算机基础教学实验中心、IBM技术中心和计算机专业实验中心。 ' ),
71
+ (' 外国语学院' , ' 四川大学外国语学院设有7个教学单位,6个文理兼收的本科专业;拥有1个一级学科博士授予点,3个二级学科博士授予点,5个一级学科硕士学位授权点,5个二级学科硕士学位授权点,5个硕士专业授权领域,同时还有2个硕士专业学位(MTI)专业;有教职员工210余人,其中教授、副教授80余人,教师中获得中国国内外名校博士学位和正在职攻读博士学位的教师比例占专任教师的60%以上。 ' ),
72
+ (' 经济管理学院' , ' 四川大学经济学院前身是创办于1905年的四川大学经济科;已故经济学家彭迪先、张与九、蒋学模、胡寄窗、陶大镛、胡代光,以及当代学者刘诗白等曾先后在此任教或学习;1905年,四川大学设经济科;1924年,四川大学经济系成立;1998年,四川大学经济管理学院变更为四川大学经济学院。 ' );
78
73
79
74
-- 插入学生数据
80
- insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values
81
- (1001 , ' 杨逍' , 1 , ' 1990-3-4' , ' 四川成都' , 1 ),
82
- (1002 , ' 任我行' , 1 , ' 1992-2-2' , ' 湖南长沙' , 1 ),
83
- (1033 , ' 王语嫣' , 0 , ' 1989-12-3' , ' 四川成都' , 1 ),
84
- (1572 , ' 岳不群' , 1 , ' 1993-7-19' , ' 陕西咸阳' , 1 ),
85
- (1378 , ' 纪嫣然' , 0 , ' 1995-8-12' , ' 四川绵阳' , 1 ),
86
- (1954 , ' 林平之' , 1 , ' 1994-9-20' , ' 福建莆田' , 1 ),
87
- (2035 , ' 东方不败' , 1 , ' 1988-6-30' , null , 2 ),
88
- (3011 , ' 林震南' , 1 , ' 1985-12-12' , ' 福建莆田' , 3 ),
89
- (3755 , ' 项少龙' , 1 , ' 1993-1-25' , null , 3 ),
90
- (3923 , ' 杨不悔' , 0 , ' 1985-4-17' , ' 四川成都' , 3 );
75
+ insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid)
76
+ values
77
+ (1001 , ' 杨逍' , 1 , ' 1990-3-4' , ' 四川成都' , 1 ),
78
+ (1002 , ' 任我行' , 1 , ' 1992-2-2' , ' 湖南长沙' , 1 ),
79
+ (1033 , ' 王语嫣' , 0 , ' 1989-12-3' , ' 四川成都' , 1 ),
80
+ (1572 , ' 岳不群' , 1 , ' 1993-7-19' , ' 陕西咸阳' , 1 ),
81
+ (1378 , ' 纪嫣然' , 0 , ' 1995-8-12' , ' 四川绵阳' , 1 ),
82
+ (1954 , ' 林平之' , 1 , ' 1994-9-20' , ' 福建莆田' , 1 ),
83
+ (2035 , ' 东方不败' , 1 , ' 1988-6-30' , null , 2 ),
84
+ (3011 , ' 林震南' , 1 , ' 1985-12-12' , ' 福建莆田' , 3 ),
85
+ (3755 , ' 项少龙' , 1 , ' 1993-1-25' , null , 3 ),
86
+ (3923 , ' 杨不悔' , 0 , ' 1985-4-17' , ' 四川成都' , 3 ),
87
+ (4040 , ' 炼腰的隔壁老王' , 1 , ' 1989-1-1' , ' 四川成都' , 2 );
88
+
89
+ -- 删除学生数据
90
+ delete from tb_student where stuid= 4040 ;
91
+
92
+ -- 更新学生数据
93
+ update tb_student set stuname= ' 杨过' , stuaddr= ' 湖南长沙' where stuid= 1001 ;
91
94
92
95
-- 插入老师数据
93
96
insert into tb_teacher (teaid, teaname, teatitle, collid) values
@@ -110,7 +113,7 @@ insert into tb_course (couid, couname, coucredit, teaid) values
110
113
(9999 , ' 审计学' , 3 , 3366 );
111
114
112
115
-- 插入选课数据
113
- insert into tb_score (stuid, couid, scdate, scmark ) values
116
+ insert into tb_record (sid, cid, seldate, score ) values
114
117
(1001 , 1111 , ' 2017-09-01' , 95 ),
115
118
(1001 , 2222 , ' 2017-09-01' , 87 .5 ),
116
119
(1001 , 3333 , ' 2017-09-01' , 100 ),
@@ -125,9 +128,9 @@ insert into tb_score (stuid, couid, scdate, scmark) values
125
128
(1378 , 1111 , ' 2017-09-05' , 82 ),
126
129
(1378 , 7777 , ' 2017-09-02' , 65 .5 ),
127
130
(2035 , 7777 , ' 2018-09-03' , 88 ),
128
- (2035 , 9999 , curdate() , null ),
129
- (3755 , 1111 , date (now()) , null ),
130
- (3755 , 8888 , date (now()) , null ),
131
+ (2035 , 9999 , default , null ),
132
+ (3755 , 1111 , default , null ),
133
+ (3755 , 8888 , default , null ),
131
134
(3755 , 9999 , ' 2017-09-01' , 92 );
132
135
133
136
-- 查询所有学生信息
@@ -227,8 +230,6 @@ select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.
227
230
228
231
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1 .stuid = t3 .stuid inner join tb_course t2 on t2 .couid = t3 .couid where scmark is not null order by scmark desc limit 10 , 5 ;
229
232
230
- -- 单表:65535TB
231
- -- 单列:4G - LONGBLOB (Binary Large OBject) / LONGTEXT
232
233
-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
233
234
select stuname, avgmark from tb_student t1, (select stuid, avg (scmark) as avgmark from tb_score group by stuid) t2 where t1 .stuid = t2 .stuid ;
234
235
0 commit comments