1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
| 1.create database CSEDB_学号;
2.创建表student(具体数据类型char的大小感觉多少就行,影响不大) mysql> create table Student( -> Sno char(9) primary key, -> Sname char(20) unique, -> Ssex char(2), -> Sage int, -> Sdept char(20), -> Scholarshhip char(2)) -> ; Query OK, 0 rows affected (0.12 sec)
mysql> show columns from student; +--------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+----------+------+-----+---------+-------+ | Sno | char(9) | NO | PRI | NULL | | | Sname | char(20) | YES | UNI | NULL | | | Ssex | char(2) | YES | | NULL | | | Sage | int | YES | | NULL | | | Sdept | char(20) | YES | | NULL | | | Scholarshhip | char(2) | YES | | NULL | | +--------------+----------+------+-----+---------+-------+ 6 rows in set (0.02 sec)
ps: 唯一键可以为空,主键不能为空
3.创建表course mysql> create table Course( -> Cno char(4) primary key, -> Cname char(40) unique, -> Cpno char(4), -> Ccredit int) -> ; Query OK, 0 rows affected (0.07 sec)
mysql> show columns from course; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | Cno | char(4) | NO | PRI | NULL | | | Cname | char(40) | YES | UNI | NULL | | | Cpno | char(4) | YES | | NULL | | | Ccredit | int | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
4.创建表sc mysql> create table sc( -> sno char(5), -> cno char(3), -> grade int, -> primary key (sno,cno) -> ); Query OK, 0 rows affected (0.03 sec)
mysql> show columns from Sc; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | Sno | char(9) | NO | PRI | NULL | | | Cno | char(4) | NO | PRI | NULL | | | Grade | int | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
5,删除表 mysql> show tables; +----------------------------+ | Tables_in_csedb_u201911749 | +----------------------------+ | course | | sc | | student | +----------------------------+ 3 rows in set (0.00 sec)
mysql> drop tables sc; Query OK, 0 rows affected (0.02 sec)
mysql> show tables; +----------------------------+ | Tables_in_csedb_u201911749 | +----------------------------+ | course | | student | +----------------------------+ 2 rows in set (0.00 sec)
6.更改表结构(增加/删除一列,更改表字段类型) mysql> show columns from student; +--------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+----------+------+-----+---------+-------+ | Sno | char(9) | NO | PRI | NULL | | | Sname | char(20) | YES | UNI | NULL | | | Ssex | char(2) | YES | | NULL | | | Sage | int | YES | | NULL | | | Sdept | char(20) | YES | | NULL | | | Scholarshhip | char(2) | YES | | NULL | | +--------------+----------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
mysql> alter table student add Scome datetime; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table student drop column scome; Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table student modify column sage smallint; Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show columns from student; +--------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+----------+------+-----+---------+-------+ | Sno | char(9) | NO | PRI | NULL | | | Sname | char(20) | YES | UNI | NULL | | | Ssex | char(2) | YES | | NULL | | | Sage | smallint | YES | | NULL | | | Sdept | char(20) | YES | | NULL | | | Scholarshhip | char(2) | YES | | NULL | | | Scome | datetime | YES | | NULL | | +--------------+----------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
7.创建索引,删除索引 mysql> create unique index stusno on student(sno); Query OK, 0 rows affected, 1 warning (0.04 sec) Records: 0 Duplicates: 0 Warnings: 1 ##第一种删除方式 mysql> drop index stusno on student; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 ##第二种删除方式 mysql> alter table student drop index stusno; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 ##第三种删除方式,删除主键索引 alter table 表名 drop primary key; ##第四种删除方式,删除外键索引 alter table 表名 drop foreign key 外键名;//未检验
8.按照指导书的要求,建立三个表之间的关系,就是设置外键 mysql> alter table course add foreign key (cpno) references course(cno); Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table sc add foreign key (sno) references student(sno); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table sc add foreign key (cno) references course(cno); Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 注意外键约束!!! ps:设置外键之后,如果想要修改父表的话,需要设置一个级联操作命令,当在操作父表时,子表会受到相同的影响,如果没有设置级联操作,更改父表会报错。 级联操作: 主表更新 语法形式为:on update[级联操作] 主表删除 语法形式为:on delete[级联操作] 这里的级联操作常见的有三种形式: cascade:同步操作,或者串联操作!也就是当主表记录删除或更新的时候,从表也进行相应的删除 更新! set null:设置为null,也就是当主表记录删除或更新的时候,从表中的外键字段设置为空 restrict:拒绝主表的更新或删除 9.添加数据(insert) 语法规则: 1. 指定向那些列插入值: insert into table_name ( field1, field2,...fieldN ) values ( value1, value2,...valueN ); 2. 如果插入的数据覆盖所有的列,则不用指定插入列的范围 insert into table_name values ( value1, value2,...valueN ); ps: 所有符号都必须是英文符号 向student表中插入数据: mysql> insert into student values('200215121','李勇','男',20,'CS','否'); Query OK, 1 row affected (0.01 sec)
mysql> insert into student values('200215122','刘晨','女',19,'CS','否'); Query OK, 1 row affected (0.00 sec)
mysql> insert into student values('200215123','王敏','女',18,'MA','否'); Query OK, 1 row affected (0.00 sec)
mysql> insert into student values('200215125','张立','男',19,'IS','否'); Query OK, 1 row affected (0.01 sec)
mysql> select * from student; +-----------+-------+------+------+-------+-------------+ | Sno | Sname | Ssex | Sage | Sdept | Scholarship | +-----------+-------+------+------+-------+-------------+ | 200215121 | 李勇 | 男 | 20 | CS | 否 | | 200215122 | 刘晨 | 女 | 19 | CS | 否 | | 200215123 | 王敏 | 女 | 18 | MA | 否 | | 200215125 | 张立 | 男 | 19 | IS | 否 | +-----------+-------+------+------+-------+-------------+ 4 rows in set (0.01 sec) 向course表中插入数据: insert into course values('1','数据库',NULL,4); insert into course values('2','数学',NULL,2); insert into course values('3','信息系统',NULL,4); insert into course values('4','操作系统',NULL,3); insert into course values('5','数据结构',NULL,4); insert into course values('6','数据处理',NULL,2); insert into course values('7','java',NULL,4); 向sc表中插入数据: insert into SC values('200215121','1',92); insert into SC values('200215121','2',85); insert into SC values('200215121','3',88); insert into SC values('200215122','2',90); insert into SC values('200215122','3',80);
10.更新数据(update) 语法规则: UPDATE [LOW_PRIORITY] [IGNORE] table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] [ORDER BY ...] [LIMIT ROW_COUNT] 更新courseb表数据: update Course set Cpno = '5' where Cno = '1'; update Course set Cpno = '1' where Cno = '3'; update Course set Cpno = '6' where Cno = '4'; update Course set Cpno = '7' where Cno = '5'; update Course set Cpno = '6' where Cno = '7';
11.删除数据(delete) 语法规则: delete from table_name [where clause] 如果没有指定where,则删除表中所有数据
mysql> select * from student; +-----------+-------+------+------+-------+-------------+ | Sno | Sname | Ssex | Sage | Sdept | Scholarship | +-----------+-------+------+------+-------+-------------+ | 200215121 | 李勇 | 男 | 20 | CS | 否 | | 200215122 | 刘晨 | 女 | 19 | CS | 否 | +-----------+-------+------+------+-------+-------------+ 2 rows in set (0.01 sec)
mysql> delete from student; Query OK, 2 rows affected (0.02 sec)
mysql> select * from student; Empty set (0.00 sec)
12.扩展练习: (1)查询全体学生的学号、姓名和年龄; mysql> select sno,sname,sage -> from student; +-----------+-------+------+ | sno | sname | sage | +-----------+-------+------+ | 200215121 | 李勇 | 20 | | 200215122 | 刘晨 | 19 | | 200215123 | 王敏 | 18 | | 200215125 | 张立 | 19 | +-----------+-------+------+ 4 rows in set (0.00 sec) (2)查询所有计算机系学生的详细记录; mysql> select * -> from student -> where sdept = 'CS'; +-----------+-------+------+------+-------+-------------+ | Sno | Sname | Ssex | Sage | Sdept | Scholarship | +-----------+-------+------+------+-------+-------------+ | 200215121 | 李勇 | 男 | 20 | CS | 否 | | 200215122 | 刘晨 | 女 | 19 | CS | 否 | +-----------+-------+------+------+-------+-------------+ 2 rows in set (0.00 sec) (3)找出考试成绩为优秀(90 分及以上)或不及格的学生的学号、课程号及成绩; mysql> select * -> from sc -> where grade >= 90 or grade <60; +-----------+-----+-------+ | Sno | Cno | Grade | +-----------+-----+-------+ | 200215121 | 1 | 92 | | 200215122 | 2 | 90 | +-----------+-----+-------+ 2 rows in set (0.00 sec) (4)查询年龄不在 19~20 岁之间的学生姓名、性别和年龄; mysql> select sname,ssex,sage -> from student -> where sage not in (19,20); +-------+------+------+ | sname | ssex | sage | +-------+------+------+ | 王敏 | 女 | 18 | +-------+------+------+ 1 row in set (0.00 sec) (5)查询数学系(MA)、信息系(IS)的学生的姓名和所在系; mysql> select sname,sdept -> from student -> where sdept in ('CS','MA'); +-------+-------+ | sname | sdept | +-------+-------+ | 李勇 | CS | | 刘晨 | CS | | 王敏 | MA | +-------+-------+ 3 rows in set (0.00 sec) (6)查询名称中包含“数据”的所有课程的课程号、课程名及其学分; mysql> select cno,cname,ccredit -> from course -> where cname like '%数据%'; +-----+----------+---------+ | cno | cname | ccredit | +-----+----------+---------+ | 1 | 数据库 | 4 | | 5 | 数据结构 | 4 | | 6 | 数据处理 | 2 | +-----+----------+---------+ 3 rows in set (0.00 sec) (7) 找出所有没有选修课成绩的学生学号和课程号; mysql> select distinct sno,cno -> from student,course -> where sno not in(select distinct sno from sc) and cno not in (select distinct cno from sc); +-----------+-----+ | sno | cno | +-----------+-----+ | 200215123 | 6 | | 200215125 | 6 | | 200215123 | 4 | | 200215125 | 4 | | 200215123 | 7 | | 200215125 | 7 | | 200215123 | 5 | | 200215125 | 5 | +-----------+-----+ 8 rows in set (0.00 sec) (8)查询学生 200215121 选修课的最高分、最低分以及平均成绩; mysql> select max(grade),min(grade),avg(grade) -> from sc -> where sno = '200215121'; +------------+------------+------------+ | max(grade) | min(grade) | avg(grade) | +------------+------------+------------+ | 92 | 85 | 88.3333 | +------------+------------+------------+ 1 row in set (0.01 sec) (9)查询选修了 2 号课程的学生的学号及其成绩,查询结果按成绩升序排列; mysql> select sno,grade -> from sc -> where cno = '2' -> order by grade; +-----------+-------+ | sno | grade | +-----------+-------+ | 200215121 | 85 | | 200215122 | 90 | +-----------+-------+ 2 rows in set (0.00 sec) (10)查询每个系名及其学生的平均年龄。 mysql> select sdept,avg(sage) -> from student -> group by sdept; +-------+-----------+ | sdept | avg(sage) | +-------+-----------+ | CS | 19.5000 | | MA | 18.0000 | | IS | 19.0000 | +-------+-----------+ 3 rows in set (0.01 sec) (思考:如何查询学生平均年龄在 19 岁以下(含 19 岁)的系别及其学生的平均年龄?) mysql> select sdept,avg(sage) -> from student -> group by sdept -> having avg(sage)<=19; +-------+-----------+ | sdept | avg(sage) | +-------+-----------+ | MA | 18.0000 | | IS | 19.0000 | +-------+-----------+ 2 rows in set (0.01 sec)
ps:where用于过滤指定的行,它后面的判断数据必须是存在的行,having用于对前面select的结果进行筛选,但having不一定非要配合group by使用,如果我前面select的结果中没有,就不能用having 总的来说,where直接对数据表中的字段进行直接筛选,而having是对前面筛选的结果进行筛选 参考链接:https://blog.csdn.net/yexudengzhidao/article/details/54924471 参考链接:https://www.jianshu.com/p/e66abfadcdca
|