DML操作

DML 只是对表内部的数据操作,不涉及表的定义,结构的修改。
主要包括(insert)(update)(delete)(select)
删除数据库、表:
drop database dbname
drop table tablename / alter table tablename drop colum 字段
修改表:
alter table tablename MODIFY[COLUMN] 字段 约束
增加表字段:
alter table tablename ADD[COLUMN] 字段 约束
修改字段名字:
alter table tablename change 字段 字段1 约束
修改字段排列顺序:
放在字段2后:
alter table tablename add 字段1 date after 字段2
放在最前面:
alter table tablename modify 字段 约束
更该表名:
alter table tablename RENAME[to]new_tablename

更新表数据:
update tablename SET file1=value1,file2=value2,...filen=valuen
where condition

更新多张表:
update t1,t2,t3...tn SET t1.file1=expr1...tn.filen=exprn
where condition

删除记录:
delete from tablename where condition
删除多个表的数据:
delete t1,t2,t3...tn from t1,t2,...tn
where condition

排序限制查询:
多条件查询:
select * from tablename
where 字段=condition and 字段<condition
排序:
select * from tablename
where condition ORDER BY file1 [DESCASC],file2[DESC],...filen[ASC]
排序限制:
select * from tablename order by file1 limit 3;

聚合:
select [file1,file2,...,filen]fun_name
from tablename
[WHERE where_condition]
[GROUP BY file1,file2,...,filen[WITH ROLLUP]]
[HAVING where_condition]

fun_name:要做的聚合操作,聚合函数有(sum)求和,count(*)(记录数),max,min
GROUP BY:关键字表示要进行分类聚合的字段
WITH ROLLUP:聚合后的结果进行再汇总
HAVING:对分类后的结果在进行条件过滤

having,where的区别在于:
having是对聚合后的结果进行调节的过滤
where是在聚合前对记录进行过滤。(先where后having效率最大)


表连接:
select file1,file2 from tablename1,tablename2
where tablename1.file=tablename2.file

左连接:

右连接:

子查询:
select * from tablename1 
where file in(select file from tablename2);
子查询转化为表连接:
select tablename.* from tablename1,tablename2
where tablename1.file=tablename2.file;

原文地址:https://www.cnblogs.com/xusx/p/6040060.html