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 [DESC\ASC],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;

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值