**一、上机内容** \1. 使用SQL语句创建数据库studentsdb。 ```sql create database studentsdb; ``` \2. 使用SQL语句选择studentsdb为当前使用数据库。 ```sql use studentsdb; ``` \3. 使用SQL语句在studentsdb数据库创建数据表student_info、curriculum、grade,三个表的数据结构如表1-表3所示。 表1 student_info表结构 | 列名 | 数据类型 | 允许NULL值 | 主键 | | -------- | ----------- | ---------- | ---- | | 学号 | char(4) | 否 | 是 | | 姓名 | char(8) | 否 | 否 | | 性别 | char(2) | 是 | 否 | | 出生日期 | date | 是 | 否 | | 家庭住址 | varchar(50) | 是 | 否 | ```sql create table student_info( 学号 char(4) not null PRIMARY KEY, 姓名 char(8) not null, 性别 char(2), 出生日期 date, 家庭住址 varchar(50) ); ``` ![image-20220923104156917](https://lsky.hhdxw.top/imghub/img/202209231042251.png) 表2 表结构 | 列名 | 数据类型 | 允许NULL值 | 主键 | | -------- | ----------- | ---------- | ---- | | 课程编号 | char(4) | 否 | 是 | | 课程名称 | varchar(50) | 是 | 否 | | 学分 | int | 是 | 否 | ```sql create table curriculum( 课程编号 char(4) not null PRIMARY KEY, 课程名称 varchar(50), 学分 int ); ``` ![截屏2022-09-23 10.43.40](https://lsky.hhdxw.top/imghub/img/%E6%88%AA%E5%B1%8F2022-09-23%2010.43.40.png) 表3 grade表结构 | 列名 | 数据类型 | 允许NULL值 | 主键 | | -------- | -------- | ---------- | ---- | | 学号 | char(4) | 否 | 是 | | 课程编号 | char(4) | 否 | 是 | | 分数 | int | 是 | 否 | ```sql create table grade( 学号 char(4) not null, 课程编号 char(4) not null, 分数 int, PRIMARY KEY(学号,课程编号) ); ``` ![截屏2022-09-23 10.44.15](https://lsky.hhdxw.top/imghub/img/%E6%88%AA%E5%B1%8F2022-09-23%2010.44.15.png) \4. 使用SQL语句INSERT向studentsdb数据库的student_info、curriculum、grade表插入数据,各表数据如表4-表6所示。 表4 student_info表的数据 | 学号 | 姓名 | 性别 | 出生日期 | 家庭住址 | | ---- | ------ | ---- | ---------- | -------------- | | 0001 | 张青平 | 男 | 2000-10-01 | 衡阳市东风一路 | | 0002 | 刘东阳 | 男 | 1998-12-09 | 东阳市八一北路 | | 0003 | 马晓夏 | 女 | 1995-05-12 | 长岭市五一路 | | 0004 | 钱忠理 | 男 | 1994-09-23 | 滨海市洞庭大道 | | 0005 | 孙海洋 | 男 | 1995-04-03 | 长岛市解放路 | | 0006 | 郭小斌 | 男 | 1997-11-10 | 南山市红旗路 | | 0007 | 肖月玲 | 女 | 1996-12-07 | 东方市南京路 | | 0008 | 张玲珑 | 女 | 1997-12-24 | 滨江市新建路 | ```sql insert into student_info(学号,姓名,性别,出生日期,家庭住址) values('0001','张青平','男','2000-10-01','衡阳市东风路77号'), ('0002','刘东阳','男','1998-12-09','东阳市八一北路33号'), ('0003','马晓夏','女','1995-05-12','长岭市五一路763号'), ('0004','钱忠理','男','1994-09-23','滨海市洞庭大道279号'), ('0005','孙海洋','男','1995-04-03','长岛市解放路27号'), ('0006','郭小斌','男','1997-11-10','南山市红旗路113号'), ('0007','肖月玲','女','1996-12-07','东方市南京路11号'), ('0008','张玲珑','女','1997-12-24','滨江市新建路97号'); ``` ![截屏2022-09-23 10.44.41](https://lsky.hhdxw.top/imghub/img/%E6%88%AA%E5%B1%8F2022-09-23%2010.44.41.png) 表5 curriculum表的数据 | 课程编号 | 课程名称 | 学分 | | -------- | ---------------- | ---- | | 0001 | 计算机应用基础 | 2 | | 0002 | C语言程序设计 | 2 | | 0003 | 数据库原理及应用 | 2 | | 0004 | 英语 | 4 | | 0005 | 高等数学 | 4 | ```sql insert into curriculum(课程编号,课程名称,学分) values('0001','计算机应用基础',2), ('0002','C语言程序设计',2), ('0003','数据库原理及应用',2), ('0004','英语',4), ('0005','高等数学',4); ``` ![截屏2022-09-23 10.45.30](https://lsky.hhdxw.top/imghub/img/%E6%88%AA%E5%B1%8F2022-09-23%2010.45.30.png) 表6 grade表的数据 | 学号 | 课程编号 | 分数 | | ---- | -------- | ---- | | 0001 | 0001 | 80 | | 0001 | 0002 | 91 | | 0001 | 0003 | 88 | | 0001 | 0004 | 85 | | 0001 | 0005 | 77 | | 0002 | 0001 | 73 | | 0002 | 0002 | 68 | | 0002 | 0003 | 80 | | 0002 | 0004 | 79 | | 0002 | 0005 | 73 | | 0003 | 0001 | 84 | | 0003 | 0002 | 92 | | 0003 | 0003 | 81 | | 0003 | 0004 | 82 | | 0003 | 0005 | 75 | ```sql insert into grade(学号,课程编号,分数) values('0001','0001',80), ('0001','0002',91), ('0001','0003',88), ('0001','0004',85), ('0001','0005',77), ('0002','0001',73), ('0002','0002',68), ('0002','0003',80), ('0002','0004',79), ('0002','0005',73), ('0003','0001',84), ('0003','0002',92), ('0003','0003',81), ('0003','0004',82), ('0003','0005',75); ``` ![image-20220923100729127](https://lsky.hhdxw.top/imghub/img/202209231007173.png) \5. 使用SQL语句ALTER TABLE修改curriculum表的“课程名称”列,使之为空。 ```sql alter table curriculum modify 课程名称 varchar(50) not null; ``` ![image-20220923104648161](https://lsky.hhdxw.top/imghub/img/image-20220923104648161.png) \6. 使用SQL语句ALTER TABLE修改grade表的“分数”列,使其数据类型为decimal(5,2)。 ```sql alter table grade modify 分数 decimal(5,2); ``` ![image-20220923104705660](https://lsky.hhdxw.top/imghub/img/image-20220923104705660.png) \7. 使用SQL语句ALTER TABLE为student_info表添加一个名为“备注”的数据列,其数据类型为varchar(50)。 ```sql alter table student_info add 备注 varchar(50); ``` ![image-20220923104721475](https://lsky.hhdxw.top/imghub/img/image-20220923104721475.png) \8. 使用SQL语句创建数据库studb,并在此数据库下创建表stu,表结构与数据与studentsdb的student_info表相同。 ```sql create database studb; use studb; create table stu as select * from studentsdb.student_info; ``` ![image-20220923104800750](https://lsky.hhdxw.top/imghub/img/image-20220923104800750.png) \9. 使用SQL语句删除表stu中学号为0004的记录。 ```sql delete from stu where 学号 = '0004'; ``` ![image-20220923104823284](https://lsky.hhdxw.top/imghub/img/image-20220923104823284.png) 10.使用SQL语句更新表stud中学号为0002的家庭住址为“滨江市新建路96号”。 ```sql update stu set 家庭住址 = '滨江市新建路96号' where 学号 = '0002' ; ``` ![image-20220923104902292](https://lsky.hhdxw.top/imghub/img/image-20220923104902292.png) 11.删除表stud的“备注”列。 ```sql alter table stu drop column 备注; ``` ![image-20220923104914868](https://lsky.hhdxw.top/imghub/img/image-20220923104914868.png) **二、实验思考** \1. 能通过一个CREATE DATABASE语句创建两个及以上的数据库吗? 不能 \2. 删除了的数据库还能可能恢复吗? 一般来说是不能恢复的 \3. 对于studentsdb数据库的student_info表而言,如果输入相同学号的记录将出现什么现象?为什么? 不能插入,导致插入冲突。 避免方式就是将学号设为主键,不能重复。 \4. 已经打开的表能删除吗? 可以