当前位置:网站首页>SQL 进阶挑战(1 - 5)
SQL 进阶挑战(1 - 5)
2022-06-13 04:01:00 【村雨遥】
SQL1 插入记录(一)
描述
牛客后台会记录每个用户的试卷作答记录到 exam_record 表,现在有两个用户的作答记录详情如下:
- 用户 1001 在 2021 年 9 月 1 日晚上 10 点 11 分 12 秒开始作答试卷 9001,并在 50分钟后提交,得了 90 分;
- 用户 1002 在2021年9月4日上午7点1分2秒开始作答试卷 9002,并在10分钟后退出了平台。
试卷作答记录表 exam_record 中,表已建好,其结构如下。
Filed | Type | Null | Key | Extra | Default | Comment |
---|---|---|---|---|---|---|
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
uid | int(11) | NO | (NULL) | 用户ID | ||
exam_id | int(11) | NO | (NULL) | 试卷ID | ||
start_time | datetime | NO | (NULL) | 开始时间 | ||
submit_time | datetime | YES | (NULL) | 提交时间 | ||
score | tinyint(4) | YES | (NULL) | 得分 |
该题最后会通过执行 SELECT uid, exam_id, start_time, submit_time, score FROM 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;
TRUNCATE exam_record;
复制
输出:
1001|9001|2021-09-01 22:11:12|2021-09-01 23:01:12|90
1002|9002|2021-09-04 07:01:02|None|None
解答
主要考察的是插入语句,其语法如下:
INSERT INTO [表名] [字段名] VALUES [记录1、记录2];
要注意的是表中的 id
是自增的,所以我们不需要插入该字段,所以此处需要指定插入记录所对应的字段,最终得到的答案如下。
INSERT INTO exam_record (uid, exam_id, start_time, submit_time, score)
VALUES
(1001, 9001, "2021-09-01 22:11:12", "2021-09-01 23:01:12", 90),
(1002, 9002, '2021-09-04 07:01:02', NULL, NULL);
当然,我们也可以不用指定字段,但是此时需要指定 id
字段的值,因为该字段设置的是自增,所以插值时只需要将其设置为 NULL
即可,最终得到的答案如下。
INSERT INTO exam_record
VALUES
(NULL, 1001, 9001, "2021-09-01 22:11:12", "2021-09-01 23:01:12", 90),
(NULL, 1002, 9002, '2021-09-04 07:01:02', NULL, NULL);
SQL2 插入记录(二)
描述
现有一张试卷作答记录表 exam_record,结构如下表,其中包含多年来的用户作答试卷记录,由于数据越来越多,维护难度越来越大,需要对数据表内容做精简,历史数据做备份。
表 exam_record:
Filed | Type | Null | Key | Extra | Default | Comment |
---|---|---|---|---|---|---|
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
uid | int(11) | NO | (NULL) | 用户ID | ||
exam_id | int(11) | NO | (NULL) | 试卷ID | ||
start_time | datetime | NO | (NULL) | 开始时间 | ||
submit_time | datetime | YES | (NULL) | 提交时间 | ||
score | tinyint(4) | YES | (NULL) | 得分 |
问题
我们已经创建了一张新表 exam_record_before_2021 用来备份 2021 年之前的试题作答记录,结构和 exam_record 表一致,请将2021年之前的已完成了的试题作答纪录导入到该表。
后台会通过执行 “SELECT * FROM exam_record_before_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;
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;
TRUNCATE exam_record;
TRUNCATE exam_record_before_2021;
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);
输出:
1|1001|9002|2020-01-02 09:01:01|2020-01-02 09:21:01|70
解答
主要考察从一个表插入到另一个表,其语法如下:
INSERT INTO [新数据表名] SELECT * FROM [旧数据表名] WHERE [条件语句];
然后套用上面的语法,将对应数据表名和字段进行替换即可。
INSERT INTO exam_record_before_2021(uid, exam_id, start_time, submit_time, score)
SELECT uid, exam_id, start_time, submit_time, score
FROM exam_record
WHERE YEAR(submit_time) < '2021';
SQL3 插入记录(三)
描述
现在有一套ID为9003的高难度SQL试卷,时长为一个半小时,请你将 2021-01-01 00:00:00 作为发布时间插入到试题信息表 examination_info(其表结构如下图)。
试题信息表 examination_info:
Filed | Type | Null | Key | Extra | Default | Comment |
---|---|---|---|---|---|---|
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
exam_id | int(11) | NO | UNI | (NULL) | 试卷ID | |
tag | varchar(32) | YES | (NULL) | 类别标签 | ||
difficulty | varchar(8) | YES | (NULL) | 难度 | ||
duration | int(11) | NO | (NULL) | 时长(分钟数) | ||
release_time | datetime | YES | (NULL) | 发布时间 |
问题
不管该ID试卷是否存在,都要插入成功,请尝试插入它。
后台会通过执行 SELECT exam_id,tag,difficulty,duration,release_time FROM examination_info 语句来对比结果。
示例
输入:
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');
输出:
9001|SQL|hard|60|2020-01-01 10:00:00
9002|算法|easy|60|2020-01-01 10:00:00
9004|算法|hard|80|2020-01-01 10:00:00
9003|SQL|hard|90|2021-01-01 00:00:00
解答
一种是曲线救国的方式,我们假设 examination_info
中有 exam_id
为 9003
的数据,先将其从数据库中删除,然后再将数据插入进去。
DELETE FROM examination_info
WHERE exam_id = 9003;
INSERT INTO examination_info
VALUES (NULL, 9003, "SQL", "hard", "90", "2021-01-01 00:00:00")
而另一种则是利用 REPLACE
语句实现带更新的插入,其语法结构如下:
REPLACE INTO [表名] VALUES (value1, value2……)
REPLACE INTO examination_info VALUES
(NULL, 9003, "SQL", "hard", 90, "2021-01-01 00:00:00");
SQL4 更新记录(一)
描述
现有一张试卷信息表examination_info,表结构如下图所示:
Filed | Type | Null | Key | Extra | Default | Comment |
---|---|---|---|---|---|---|
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
exam_id | int(11) | NO | UNI | (NULL) | 试卷ID | |
tag | char(32) | YES | (NULL) | 类别标签 | ||
difficulty | char(8) | YES | (NULL) | 难度 | ||
duration | int(11) | NO | (NULL) | 时长 | ||
release_time | datetime | YES | (NULL) | 发布时间 |
问题
请把 examination_info 表中 tag 为 PYTHON 的 tag 字段全部修改为 Python。
后台会通过执行 'SELECT exam_id,tag,difficulty,duration,release_time FROM examination_info;'语句来对比结果。
示例
输入:
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');
输出:
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
解答
主要考察更新语句。
UPDATE 表名 SET 字段1=‘替换内容1’,字段2=‘替换内容3’ WHERE ....
将标签满足为 PYTHON
的记录的 tag
全部更新为 Python
即可。
UPDATE examination_info
SET tag = "Python"
WHERE tag = "PYTHON";
SQL5 更新记录(二)
描述
现有一张试卷作答记录表exam_record,其中包含多年来的用户作答试卷记录,结构如下表:
作答记录表exam_record:
submit_time为 完成时间
Filed | Type | Null | Key | Extra | Default | Comment |
---|---|---|---|---|---|---|
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
uid | int(11) | NO | (NULL) | 用户ID | ||
exam_id | int(11) | NO | (NULL) | 试卷ID | ||
start_time | datetime | NO | (NULL) | 开始时间 | ||
submit_time | datetime | YES | (NULL) | 提交时间 | ||
score | tinyint(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);
输出:
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|2099-01-01 00:00:00|0
1002|9002|2021-09-05 19:01:01|2021-09-05 19:40:01|89
1003|9001|2021-09-02 12:01:01|None|None
1003|9002|2021-09-01 12:01:01|None|None
解答
同样主要考察更新语句:
UPDATE [表名] SET 字段名="新值" WHERE 条件语句;
根据题意,要将满足条件的记录全部改成被动完成即可。
UPDATE exam_record
SET submit_time='2099-01-01 00:00:00', score = 0
WHERE start_time < '2021-09-01 00:00:00' AND score IS NULL;
边栏推荐
- Lambda终结操作查找与匹配findFirst
- Configuration and practice of shardingsphere JDBC sub database separation of read and write
- Common array methods in JS (map, filter, foreach, reduce)
- Single chip microcomputer: main index of a/d (analog-to-digital conversion)
- Lambda end operation count
- ROS话题与节点
- [web] cookies and sessions
- Lambda end operation collect
- [MySQL] index and transaction
- SCM signal generator program
猜你喜欢
Introduction to MCU peripherals: temperature sensor DS18B20
单片机:NEC 协议红外遥控器
How to use debounce in lodash to realize anti shake
单片机外设介绍:温度传感器 DS18B20
单片机:红外遥控通信原理
GoFrame第四天
No more! Another member of the team left..
Single chip microcomputer: MODBUS multi computer communication program design
基于PHP的轻量数码商城系统
Difference between OKR and KPI
随机推荐
[test development] fundamentals of software testing
Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
The most detailed swing transformer mask of window attachment in history -- Shaoshuai
Big Five personality learning records
Intervention analysis + pseudo regression
Alipay native components (hotel time selection)
大疆无人机飞控系统的原理、组成及各传感器的作用
Modeling discussion series 143 data processing, analysis and decision system development
高等数学(第七版)同济大学 习题1-2 个人解答
Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
[test development] automated test selenium (II) -- common APIs for webdriver
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
Lambda end operation count
Fundamentals of robot obstacle avoidance system
高等数学(第七版)同济大学 习题1-3 个人解答
Lambda termination operation find and match nonematch
大五人格学习记录
Stream流的注意事项
基于PHP的轻量数码商城系统
Sword finger offer II 022 Entry node of a link in a linked list