插入操作
1、插入单条记录
INSERT INTO 表名(字段名1,字段名2,...) VALUES (值 1,值 2,...);
insert into user (id,name,age) values (1,'bob',16);
2、插入多条记录
INSERT INTO 表名 (字段名1,字段名2,...)VALUES (值 1,值 2,…),(值 1,值 2,…),...;
insert into user (id,name,age) values (2,'zs',18),(3,'ls',20);
3、数据恢复插入
# 把删除的用户表数据恢复到原始表
insert into user select * from user_delete where id in (1,2,3);
4、插入,根据主键或者唯一索引判断该条记录是否已存在;已存在,那么执行更新操作
INSERT INTO exam_student` (
`student_id`,`exam_paper_id`,`sub_score`,`obj_score`,`score`,`max_id`)
values
<foreach collection="list" item="temp" separator="," index="index">
(#{temp.studentId},#{temp.examPaperId}, #{temp.subScore},#{temp.objScore},#{temp.score},#{temp.maxId})
</foreach>
on duplicate key update student_id = VALUES(student_id),
exam_paper_id = VALUES(exam_paper_id),
sub_score = VALUES(sub_score),
obj_score = VALUES(obj_score),
score = VALUES(score),
max_id = VALUES(max_id)
说明:
如果更新,需要更新的字段为 on duplicate key updat 后面的字段
删除操作
1、根据条件删除表中记录
delete from `table` where xxxx
2、全表删除
方式一:TRUNCATE `table`;
方式二:delete from `table` ; # 不加where条件
区别:
使用TRUNCATE语句删除表中的数据后,再次向表中添加记录时自动增加字段的默认初始值重新由1开始;
使用DELETE语句删除表中所有记录后,再次向表中添加记录时自动增加字段的值为删除时该字段的最大值加1
更新操作
1、单条数据更新
update `table` set field = value where xxxx;
同理,更新多个字段
update user set age=30,name='ergou' where id=1;
2、多条数据更新,在业务中,一般使用动态sql
# 示例1:更新某个年级,某些班级下 del_flag 为0
update `table` set del_flag = 0 where grade_id = #{gradeId}
<foreach collection="classNums" separator="," item="classNum" open=" and class_num IN (" close=")">
#{classNum}
</foreach>
# 示例2:在id=xxx记录中,如果字段1不等于value1,或者字段2不等于value2,则更新此条数据的字段1和字段2;实际还是通过动态sql,单条记录,循环更新
<foreach collection="list" item="item" separator=";">
UPDATE `table` set field1 = #{item.value1},
field2 = #{item.value2}
where id = #{item.id} and (filed1 != #{item.value1} or filed2 != #{item.value2})
</foreach>
3、联表更新
UPDATE `table1` a
INNER join(
select field1,field12,field3
from `table2` b
-- where xxxxxxxxxxx
-- ORDER BY xxxxx
) c
on c.field1 = a.field1 and c.field2 = a.field2
set a.field3 = c.field3
-- where xxxxxxxxxxxx
查询操作
1、基础查询
select * from `table`
# 条件查询
1、关系运算符: > < != = ...
2、关键字:IN、BETWEEN AND、AND、or
3、空值查询:is null、is not null
4、模糊查询:like; 通配符 % _
# 分组
group by
# 排序
order by
# 数量限制
limit
# 联表查询 : 关键字 on
inner join
left join
# 常用函数:
聚合函数、分组函数、字符串函数、日期时间函数、数学函数、窗口函数
触发器
# 触发器创建
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE} ON table_name
FOR EACH ROW
BEGIN
-- 触发器逻辑 ;
END
# 创建触发器-删除时插入数据
CREATE TRIGGER trigger_update_user
BEFORE UPDATE ON `user`
FOR EACH ROW
IF
OLD.del_flag = 0 and NEW.del_flag = 1
then
insert into `table`(field1,field2) values (OLD.field1,OLD.field2);
END IF
# 如果表中 score字段或者student_id字段有变化,则触发器添加一条数据到 table 中
IF NEW.score <> OLD.score OR NEW.student_id <> OLD.student_id THEN
insert into `table` (fiele1,field2) VALUES (OLD.fiele1,OLD.field2);END IF
# 展示所有触发器
show TRIGGERs
执行语句查询
# performance_schema 性能分析
# information_schema 元数据
# 查询当前正在执行的sql: information_schema.processlist
select * from information_schema.processlist where time > 1 and DB = 'xxx'
# 结束某条sql指令
kill id