数据库
首页 > 数据库> > Mysql面试题库

Mysql面试题库

作者:互联网

SQL插入

SQL 插入记录(一)

描述
现在有一套ID为9003的高难度SQL试卷,时长为一个半小时,请你将 2021-01-01 00:00:00 作为发布时间插入到试题信息表examination_info(其表结构如下图),不管该ID试卷是否存在,都要插入成功,请尝试插入它。
试题信息表examination_info
在这里插入图片描述
预计结果:

9003|SQL|hard|90|2021-01-01 00:00:00

执行语句(方法一):

replace into examination_info values(null,9003,'SQL','hard',90,'2021-01-01 00:00:00')

执行语句(方法二):

delete from examination_info
where exam_id=9003;
insert into examination_info
value(null,9003,'SQL','hard',90,'2021-01-01 00:00:00');

总结:使用insert into只能添加没有的数据,可以使用replace into替换
或者先删除在添加

SQL 更新记录(一)

描述
现有一张试卷信息表examination_info,表结构如下图所示:
在这里插入图片描述
请把examination_info表中tag为PYTHON的tag字段全部修改为Python。
执行语句(方法一):

update examination_info
set tag='Python'
where tag='PYTHON'

SQL5 更新记录(二)

描述
现有一张试卷作答记录表exam_record,其中包含多年来的用户作答试卷记录,结构如下表:
作答记录表exam_record:
submit_time为 完成时间
在这里插入图片描述
请把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);

执行语句(方法一):

update exam_record
set submit_time='2099-01-01 00:00:00' ,score=0
where start_time <'2021-09-01 00:00:00' and submit_time is null

标签:info,00,01,exam,09,面试,2021,Mysql,题库
来源: https://blog.csdn.net/t15310904876/article/details/123027284