
SQL链接
插入记录的方式汇总:考察INSERT INTO tab_name (col_name) VALUES
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);
考察INSERT INTO table_name SELECt * FROM table_name2 [WHERe key=value]
# 条件: # 2021年之前的 # 已完成了的试题作答纪录 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'
# # 方法一 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 values(null, 9003,'SQL','hard',90,'2021-01-01 00:00:00')
replace into跟 insert into功能类似,不同点在于:replace into 首先尝试插入数据到表中,
SQL链接
修改记录的方式汇总:# 第一种 update examination_info set tag = 'Python' where tag = 'PYTHON'; # 第二种 update examination_info set tag = replace(tag, 'PYTHON','Python') where tag = 'PYTHON';
第二种方式不仅可用于整体替换,还能做子串替换,例如要实现将tag中所有的PYTHON替换为Python(如CPYTHON=>CPython),可写作:
UPDATE examination_info SET tag = REPLACE(tag, "PYTHON", "Python") WHERe tag LIKE "%PYTHON%";5、将完成时间改为’2099-01-01 00:00:00’,分数改为0
# 条件 # 2021年9月1日之前开始作答 # 只改未完成的记录 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;SQL6-8 删除记录
SQL链接
删除记录的方式汇总:delete from exam_record where timestampdiff(minute,start_time,submit_time )<5 and score < 60
知识点:
时间差:
TIMESTAMPDIFF(interval, time_start, time_end)可计算time_start-time_end的时间差,单位以指定的interval为准,常用可选:
delete from exam_record
where submit_time is null
or timestampdiff(minute,start_time,submit_time) < 5
order by start_time
limit 3;
考察:
全部删除(表清空,包含自增计数器重置):TRUNCATE tb_name
# 条件 # 1、删除exam_record表中所有记录 # 2、重置自增主键 truncate exam_record二、表与索引操作 表的创建、修改与删除:
1.1 直接创建表:
CREATE TABLE [IF NOT EXISTS] tb_name -- 不存在才创建,存在就跳过 (column_name1 data_type1 -- 列名和类型必选 [ PRIMARY KEY -- 可选的约束,主键 | FOREIGN KEY -- 外键,引用其他表的键值 | AUTO_INCREMENT -- 自增ID | COMMENT comment -- 列注释(评论) | DEFAULT default_value -- 默认值 | UNIQUE -- 唯一性约束,不允许两条记录该列值相同 | NOT NULL -- 该列非空 ], ... ) [CHARACTER SET charset] -- 字符集编码 [COLLATE collate_value] -- 列排序和比较时的规则(是否区分大小写等)
1.2 从另一张表复制表结构创建表: CREATE TABLE tb_name LIKE tb_name_old
1.3 从另一张表的查询结果创建表: CREATE TABLE tb_name AS SELECt * FROM tb_name_old WHERe options
2.1 修改表:ALTER TABLE 表名 修改选项 。选项集合:
{ ADD COLUMN <列名> <类型> -- 增加列
| CHANGE COLUMN <旧列名> <新列名> <新列类型> -- 修改列名或类型
| ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT } -- 修改/删除 列的默认值
| MODIFY COLUMN <列名> <类型> -- 修改列类型
| DROP COLUMN <列名> -- 删除列
| RENAME TO <新表名> -- 修改表名
| CHARACTER SET <字符集名> -- 修改字符集
| COLLATE <校对规则名> } -- 修改校对规则(比较和排序时用到)
3.1 删除表:DROP TABLE [IF EXISTS] 表名1 [ ,表名2]
SQL9-11 表的创建、修改与删除SQL链接
9、创建一张新表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 '注册时间'
)charset = utf8 collate utf8_general_ci;
知识点: