数据库系统原理与应用教程(067)—— MySQL 练习题:操作题 82-89:数据的增删改操作

Posted 睿思达DBA_WGX

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库系统原理与应用教程(067)—— MySQL 练习题:操作题 82-89:数据的增删改操作相关的知识,希望对你有一定的参考价值。

数据库系统原理与应用教程(067)—— mysql 练习题:操作题 82-89(十一):数据的增、删、改操作

82、插入数据记录(1)

数据表:exam_record,表结构如下:

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
uidint(11)NO(NULL)用户ID
exam_idint(11)NO(NULL)试卷ID
start_timedatetimeNO(NULL)开始时间
submit_timedatetimeYES(NULL)提交时间
scoretinyint(4)YES(NULL)得分

现有两个用户的答题记录详情如下:

(1)用户 1001 在 2021 年 9 月 1 日晚上 10 点 11 分 12 秒开始作答试卷 9001,并在 50 分钟后提交,得了 90 分;

(2)用户 1002 在 2021 年 9 月 4 日上午 7 点 1 分 2 秒开始作答试卷 9002,并在 10 分钟后退出了平台。

表 exam_record 的结构:

/*
drop table if EXISTS exam_record;
CREATE TABLE IF NOT EXISTS exam_record (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;
*/

解答:

/*
insert into exam_record(uid, exam_id, start_time, submit_time, score) 
values(1001, 9001, '2021-9-1 22:11:12', '2021-9-1 23:01:12', 90);
insert into exam_record(uid, exam_id, start_time, submit_time, score) 
values(1002, 9002, '2021-9-4 7:1:2', '2021-9-4 7:11:2', null);
*/

mysql> insert into exam_record(uid, exam_id, start_time, submit_time, score) 
    -> values(1001, 9001, '2021-9-1 22:11:12', '2021-9-1 23:01:12', 90);
:11:2', null);Query OK, 1 row affected (0.00 sec)

mysql> insert into exam_record(uid, exam_id, start_time, submit_time, score) 
    -> values(1002, 9002, '2021-9-4 7:1:2', '2021-9-4 7:11:2', null);
Query OK, 1 row affected (0.00 sec)

mysql> select * from exam_record;
+----+------+---------+---------------------+---------------------+-------+
| id | uid  | exam_id | start_time          | submit_time         | score |
+----+------+---------+---------------------+---------------------+-------+
|  1 | 1001 |    9001 | 2021-09-01 22:11:12 | 2021-09-01 23:01:12 |    90 |
|  2 | 1002 |    9002 | 2021-09-04 07:01:02 | 2021-09-04 07:11:02 |  NULL |
+----+------+---------+---------------------+---------------------+-------+
2 rows in set (0.00 sec)

83、插入数据记录(2)

数据表:exam_record,结构如下。

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
uidint(11)NO(NULL)用户ID
exam_idint(11)NO(NULL)试卷ID
start_timedatetimeNO(NULL)开始时间
submit_timedatetimeYES(NULL)提交时间
scoretinyint(4)YES(NULL)得分

表 exam_record 中包含多年来用户作答试卷记录。创建一张新表 exam_record_before_2021(表结构和 exam_record 表一致) 用来备份 2021 年之前的试题作答记录,请将 2021 年之前的已完成了的试题作答纪录导入到该表。

表结构及数据如下:

/*
drop table if EXISTS exam_record;

CREATE TABLE IF NOT EXISTS exam_record (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

drop table if EXISTS exam_record_before_2021;
CREATE TABLE IF NOT EXISTS exam_record_before_2021 (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-01 09:00:01', null, null),
(1001, 9002, '2020-01-02 09:01:01', '2020-01-02 09:21:01', 70),
(1001, 9002, '2020-09-02 09:00:01', null, null),
(1002, 9001, '2021-05-02 10:01:01', '2021-05-02 10:30:01', 81),
(1002, 9002, '2021-09-02 12:01:01', null, null);
*/

解答:

/*
insert into exam_record_before_2021
select * from exam_record 
where start_time <= '2021-1-1' and submit_time is not null;
*/
mysql> select * from exam_record_before_2021;
+----+------+---------+---------------------+---------------------+-------+
| id | uid  | exam_id | start_time          | submit_time         | score |
+----+------+---------+---------------------+---------------------+-------+
|  2 | 1001 |    9002 | 2020-01-02 09:01:01 | 2020-01-02 09:21:01 |    70 |
+----+------+---------+---------------------+---------------------+-------+
1 row in set (0.00 sec)

84、插入数据记录(3)

试题信息表:examination_info,表结构如下:

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
exam_idint(11)NOUNI(NULL)试卷ID
tagvarchar(32)YES(NULL)类别标签
difficultyvarchar(8)YES(NULL)难度
durationint(11)NO(NULL)时长(分钟数)
release_timedatetimeYES(NULL)发布时间

现有一套 ID 为 9003 的高难度 SQL 试卷,时长为一个半小时,请将 2021-01-01 00:00:00 作为发布时间插入到试题信息表(examination_info),不管该 ID 试卷是否存在,都要插入成功。

表结构及数据如下:

/*
drop table if EXISTS examination_info;
CREATE TABLE IF NOT EXISTS examination_info (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
tag varchar(32) COMMENT '类别标签',
difficulty varchar(8) COMMENT '难度',
duration int NOT NULL COMMENT '时长(分钟数)',
release_time datetime COMMENT '发布时间'
)CHARACTER SET utf8 COLLATE utf8_bin;
TRUNCATE examination_info;
INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
(9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
(9002, '算法', 'easy', 60, '2020-01-01 10:00:00'),
(9003, 'SQL', 'medium', 60, '2020-01-02 10:00:00'),
(9004, '算法', 'hard', 80, '2020-01-01 10:00:00');
*/
mysql> select * from examination_info;
+----+---------+--------+------------+----------+---------------------+
| id | exam_id | tag    | difficulty | duration | release_time        |
+----+---------+--------+------------+----------+---------------------+
|  1 |    9001 | SQL    | hard       |       60 | 2020-01-01 10:00:00 |
|  2 |    9002 | 算法   | easy       |       60 | 2020-01-01 10:00:00 |
|  3 |    9003 | SQL    | medium     |       60 | 2020-01-02 10:00:00 |
|  4 |    9004 | 算法   | hard       |       80 | 2020-01-01 10:00:00 |
+----+---------+--------+------------+----------+---------------------+
4 rows in set (0.00 sec)

解答:

/*
replace into examination_info(exam_id, tag, difficulty, duration, release_time)
values(9003, 'SQL', 'hard', 90, '2021-01-01 00:00:00');
*/
mysql> replace into examination_info(exam_id, tag, difficulty, duration, release_time)
    -> values(9003, 'SQL', 'hard', 90, '2021-01-01 00:00:00');
Query OK, 2 rows affected (0.00 sec)

mysql> select * from examination_info;
+----+---------+--------+------------+----------+---------------------+
| id | exam_id | tag    | difficulty | duration | release_time        |
+----+---------+--------+------------+----------+---------------------+
|  1 |    9001 | SQL    | hard       |       60 | 2020-01-01 10:00:00 |
|  2 |    9002 | 算法   | easy       |       60 | 2020-01-01 10:00:00 |
|  4 |    9004 | 算法   | hard       |       80 | 2020-01-01 10:00:00 |
|  5 |    9003 | SQL    | hard       |       90 | 2021-01-01 00:00:00 |
+----+---------+--------+------------+----------+---------------------+
4 rows in set (0.00 sec)

85、更新数据记录(1)

现有一张试卷信息表:examination_info,表结构如下:

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
exam_idint(11)NOUNI(NULL)试卷ID
tagchar(32)YES(NULL)类别标签
difficultychar(8)YES(NULL)难度
durationint(11)NO(NULL)时长
release_timedatetimeYES(NULL)发布时间

请把 examination_info 表中 tag 列为【PYTHON】的数据全部修改为【Python】。

表结构及数据如下:

/*
drop table if EXISTS examination_info;
CREATE TABLE IF NOT EXISTS examination_info (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
tag varchar(32) COMMENT '类别标签',
difficulty varchar(8) COMMENT '难度',
duration int NOT NULL COMMENT '时长',
release_time datetime COMMENT '发布时间'
)CHARACTER SET utf8 COLLATE utf8_bin;
TRUNCATE examination_info;
INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
(9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
(9002, 'python', 'easy', 60, '2020-01-01 10:00:00'),
(9003, 'Python', 'medium', 80, '2020-01-01 10:00:00'),
(9004, 'PYTHON', 'hard', 80, '2020-01-01 10:00:00');
*/

解答:

mysql> select * from examination_info;
+----+---------+--------+------------+----------+---------------------+
| id | exam_id | tag    | difficulty | duration | release_time        |
+----+---------+--------+------------+----------+---------------------+
|  1 |    9001 | SQL    | hard       |       60 | 2020-01-01 10:00:00 |
|  2 |    9002 | python | easy       |       60 | 2020-01-01 10:00:00 |
|  3 |    9003 | Python | medium     |       80 | 2020-01-01 10:00:00 |
|  4 |    9004 | PYTHON | hard       |       80 | 2020-01-01 10:00:00 |
+----+---------+--------+------------+----------+---------------------+
4 rows in set (0.00 sec)

mysql> update examination_info set tag = 'Python' where tag = 'PYTHON';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from examination_info;
+----+---------+--------+------------+----------+---------------------+
| id | exam_id | tag    | difficulty | duration | release_time        |
+----+---------+--------+------------+----------+---------------------+
|  1 |    9001 | SQL    | hard       |       60 | 2020-01-01 10:00:00 |
|  2 |    9002 | python | easy       |       60 | 2020-01-01 10:00:00 |
|  3 |    9003 | Python | medium     |       80 | 2020-01-01 10:00:00 |
|  4 |    9004 | Python | hard       |       80 | 2020-01-01 10:00:00 |
+----+---------+--------+------------+----------+---------------------+
4 rows in set (0.00 sec)

86、更新数据记录(2)

现有一张试卷答题记录表:exam_record,表结构如下:

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
uidint(11)NO(NULL)用户ID
exam_idint(11)NO(NULL)试卷ID
start_timedatetimeNO(NULL)开始时间
submit_timedatetimeYES(NULL)提交时间
scoretinyint(4)YES(NULL)得分

其中包含多年来的用户答题记录,请把 exam_record 表中 2021 年 9 月 1 日之前开始作答的未完成记录全部改为被动完成。即:将完成时间改为 ‘2099-01-01 00:00:00’,分数改为 0。

表结构及数据如下:

/*
drop table if EXISTS exam_record;
CREATE TABLE IF NOT EXISTS exam_record (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-02 09:01:01', '2020-01-02 09:21:01', 80),
(1001, 9002, '2021-09-01 09:01:01', '2021-09-01 09:21:01', 90),
(1002, 9001, '2021-08-02 19:01:01', null, null),
(1002, 9002, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 89),
(1003, 9001, '2021-09-02 12:01:01', null, null),
(1003, 9002, '2021-09-01 12:01:01', null, null);
*/

解答:

mysql> select * from exam_record;
+----+------+---------+---------------------+---------------------+-------+
| id | uid  | exam_id | start_time          | submit_time         | score |
+----+------+---------+---------------------+---------------------+-------+
|  1 | 1001 |    9001 | 2020-01-02 09:01:01 | 2020-01-02 09:21:01 |    80 |
|  2 | 1001 |    9002 | 2021-09-01 09:01:01 | 2021-09-01 09:21:01 |    90 |
|  3 | 1002 |    9001 | 2021-08-02 19:01:01 | NULL                |  NULL |
|  4 | 1002 |    9002 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 |    89 |
|  5 | 1003 |    9001 | 2021-09-02 12:01:01 | NULL                |  NULL |
|  6 | 1003 |    9002 | 2021-09-01 12:01:01 | NULL                |  NULL |
+----+------+---------+---------------------+---------------------+-------+
6 rows in set (0.00 sec)

/*
update exam_record set submit_time = '2099-01-01 0:0:0', score = 0
where start_time < '2021-9-1 0:0:0' and submit_time is null;
*/

mysql> update exam_record set submit_time = '2099-01-01 0:0:0', score = 0
    -> where start_time < '2021-9-1 0:0:0' and submit_time is null;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from exam_record;
+----+------+---------+---------------------+---------------------+-------+
| id | uid  | exam_id | start_time          | submit_time         | score |
+----+------+---------+---------------------+---------------------+-------+
|  1 | 1001 |    9001 | 2020-01-02 09:01:01 | 2020-01-02 09:21:01 |    80 |
|  2 | 1001 |    9002 | 2021-09-01 09:01:01 | 2021-09-01 09:21:01 |    90 |
|  3 | 1002 |    9001 | 2021-08-02 19:01:01 | 2099-01-01 00:00:00 |     0 |
|  4 | 1002 |    9002 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 |    89 |
|  5 | 1003 |    9001 | 2021-09-02 12:01:01 | NULL                |  NULL |
|  6 | 1003 |    9002 | 2021-09-01 12:01:01 | NULL                |  NULL |
+----+------+---------+---------------------+---------------------+-------+
6 rows in set (0.00 sec)

87、删除记录(1)

现有一张试卷答题记录表:exam_record,表结构如下表:

FiledTypeNullKeyExtraDefaultComment
idint(11)NOPRIauto_increment(NULL)自增ID
uidint(11)NO(NULL)用户ID
exam_idint(11)NO(NULL)试卷ID
start_timedatetimeNO(NULL)开始时间
submit_timedatetimeYES(NULL)提交时间
scoretinyint(4)YES(NULL)得分

【问题】请删除 exam_record 表中答题时间小于 5 分钟整且分数不及格(及格线为60分)的记录;

表结构及数据如下:

/*
drop table if EXISTS exam_record;
CREATE TABLE IF NOT EXISTS exam_record (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;
TRUNCATE exam_record;
INSERT INTO exam_record(uid, exam_id, start_time, submit_time, score) VALUES
(1001, 9001, '2020-01-01 22:11:12', '2020-01-01 23:16:12', 50),
(1001, 9002, '2020-01-02 09:01:01', '2020-01-02 09:06:00', 58),
(1002, 9001, '2021-05-02 10:01:01', '2021-05-02 10:05:58', 60),
(1002, 9002, '2021-06-02 19:01:01', '2021-06-02 19:05:01', 54),
(1003, 9001, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 49),
(1003, 9001, '2021-09-05 19:01:01', '2021-09-05 19:15:01', 70),
(1003, 9001, '2021-09-06 19:01:01', '2021-09-06 19:05:01', 80),
(1003, 9002, '2021-09-09 07:01:02', null, null);
*/

解答:

mysql> select * from exam_record;
+----+------+---------+---------------------+---------------------+-------+
| id | uid  | exam_id | start_time          | submit_time         | score |
+----+------+---------+---------------------+---------------------+-------+
|  1 | 1001 |    9001 | 2020-01-01 22:11:12 | 2020-01-01 23:16:12 |    50 |
|  2 | 1001 |    9002 | 2020-01-02 09:01:01 | 2020-01-02 09:06:00 |    58 |
|  3 | 1002 |    9001 | 2021-05-02 10:01:01 | 2021-05-02 10:05:58 |    60 |
|  4 | 1002 |    9002 | 2021-06-02 19:01:01 | 2021-06-02 19:05:01 |    54 |
|  5 | 1003 |    9001 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 |    49 |
|  6 | 1003 |    9001 | 2021-09-05 19:01:01 | 2021-09-05 19:15:01 |    70 |
|  7 | 1003 |    9001 | 2021-09-06 19:01:01 | 2021-09-06 19:05:01 |    80 |
|  8 | 1003 |    9002 | 2021-09-09 07:以上是关于数据库系统原理与应用教程(067)—— MySQL 练习题:操作题 82-89:数据的增删改操作的主要内容,如果未能解决你的问题,请参考以下文章

全面深入理解MySQL底层原理与性能调优

视频教程 | MySQL底层原理与性能调优

MYSQL原理设计与应用

MySQL原理设计与应用

DataBase ❀ MySQL在Linux系统中的部署与安装教程

DataBase ❀ MySQL在Linux系统中的部署与安装教程