0
点赞
收藏
分享

微信扫一扫

SQL刷题系列之进阶篇

Alex富贵 2022-02-26 阅读 34

SQL刷题系列之进阶篇

增删改操作

SQL1 插入记录(一)

在这里插入图片描述
在这里插入图片描述

https://blog.nowcoder.net/n/1d9a07c9cf0c4417804f0048be4e745f

其实通过输出,我们可以知道插入数据的值。

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 
);

也可以使用 interval 对时间进行加减运算。
单独的interval需要跟在加减运算符后,如果使用date_add()或date_sub()则可以省略

insert into exam_record
VALUES (null,1001,9001,'2021-09-01 22:11:12',
'2021-09-01 22:11:12' +INTERVAL 50 minute,90),
(null,1002,9002,'2021-09-04 07:01:02',null,NULL);

SQL2 插入记录(二)

在这里插入图片描述在这里插入图片描述
题解 -> 链接
在这里插入图片描述

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 submit_time < '2021-01-01';

SQL3 插入记录(三)

在这里插入图片描述
在这里插入图片描述

REPLACE INTO examination_info
VALUES(NULL,9003,'SQL','hard',90,'2021-01-01 00:00:00');

SQL4 更新记录(一)

在这里插入图片描述
在这里插入图片描述

题解 -> 链接
在这里插入图片描述

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

SQL5 更新记录(二)

在这里插入图片描述

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

SQL6 删除记录(一)(timestampdiff 计算时间差)

在这里插入图片描述
题解 ->链接
在这里插入图片描述

delete from exam_record
where timestampdiff(minute, start_time, submit_time) < 5
and score < 60;

SQL7 删除记录(二)

在这里插入图片描述
与上一题类似,增加了排序,取前n位的操作。

delete from exam_record
where 
timestampdiff(minute, start_time, submit_time) < 5
or submit_time is null
order by start_time
limit 3;

SQL8 删除记录(三)

在这里插入图片描述
Tips:
DROP TABLE, TRUNCATE TABLE, DELETE TABLE 。三种删除语句的区别

在这里插入图片描述
根据题意,选择使用 truncate table

truncate table exam_record;

表与索引 操作

SQL9 创建一张新表

在这里插入图片描述
补充小知识:
题解 -> 链接
在这里插入图片描述
按要求填数据及类型等等,再注意些小细节就ok了。
在这里插入图片描述

 CREATE TABLE IF NOT EXISTS user_info_vip (
    id int(11) not null primary key auto_increment comment '自增ID',
    uid int(11) not null unique key comment '用户ID',
    nick_name varchar(64) comment '昵称',
    achievement int(11) default 0 comment '成就值',
    level int(11) comment '用户等级',
    job varchar(32) comment '职业方向',
    register_time datetime default CURRENT_TIMESTAMP comment '注册时间'
    )default charset=utf8;

SQL10 修改表

在这里插入图片描述
在这里插入图片描述
题解->链接
在这里插入图片描述
after 添加在那一列后,不加 after 则默认添加在最后;
change 修改列名、类型;
modify 修改数据类型、约束条件。

ALTER TABLE user_info ADD school varchar(15) AFTER level;
ALTER TABLE user_info CHANGE job profession varchar(10);
ALTER TABLE user_info modify achievement int DEFAULT 0;

SQL11 删除表

在这里插入图片描述
大意了,没想到就是这么的朴实无华。

drop table if exists exam_record_2011,exam_record_2012,exam_record_2013,exam_record_2014;

SQL12 创建索引

在这里插入图片描述
关于索引的小知识:链接
在这里插入图片描述
在这里插入图片描述
题解 -> 链接
在这里插入图片描述

create index idx_duration on examination_info(duration);
create unique index uniq_idx_exam_id on examination_info(exam_id);
create fulltext index full_idx_tag on examination_info(tag);

SQL13 删除索引

在这里插入图片描述
方法一:

alter table examination_info drop index uniq_idx_exam_id;
alter table examination_info drop index full_idx_tag;

方法二:

drop index uniq_idx_exam_id on examination_info;
drop index full_idx_tag  on examination_info;

SQL14 SQL类别高难度试卷得分的截断平均值

在这里插入图片描述
在这里插入图片描述
做题思路:

select tag, difficulty,
       ROUND((sum(score)-max(score)-min(score)) / (COUNT(score)-2), 1) AS clip_avg_score
from examination_info ei join exam_record er
on ei.exam_id=er.exam_id
where ei.tag='SQL' and difficulty='hard'

SQL15 统计作答次数

在这里插入图片描述

SELECT 
    COUNT(exam_id) AS total_pv, 
    COUNT(submit_time) AS complete_pv, 
    COUNT(DISTINCT exam_id AND score is not null) AS complete_exam_cnt
FROM exam_record

SQL16 得分不小于平均分的最低分

在这里插入图片描述
未完待续…

举报

相关推荐

0 条评论