Mysql学习总结(DDL、DML、DCL语句)

1.DDL数据库定义语句
(1)create、drop、alert、insert、update、delete

create table tableName(字段 属性,字段 属性,...);

drop table tableName;

alert table tableName modify 字段 属性 [first/after 字段]; //修改字段属性
alert table tableName add 字段 属性 [first/after 字段]; //增加字段
alert table tableName drop 字段; //删除字段
alert table tableName change 字段名 新的字段名 [first/after 字段]; //修改字段名
alert table 表名 rename 新的表名; //修改表名

2.DML数据库操作语句

增:
insert into tableName values(值),(值),...;

改:
update tableName set 字段='值' where 字段='值'; //单表更新
update a,b from tableName1 a,tableName2 b set a.字段='值' where a.字段 = b.字段; //多表更新

删:
delete from tableName where 字段='值';
delete a,b from tabelName1 a,tableName2 b where 条件; //删除多表

查询:
(1)去重关键字distinct
select id distinct from table;

(2)条件查询where
select * from table where id = 'number'

(3)排序与限制 order by 字段 ASC/DESC
select * from table order by 字段 DESC,其他字段 DESC

(4)聚合
select [字段,字段...] 聚合函数
from table
[where 条件]
[group by 字段]
[with rollup]
[having by 条件]

(5)表连接
左连接:显示左表的内容甚至右表的没有能和左表匹配的记录
右连接:小同上
select * from leftTable left join rightTable on 匹配条件

(6)子查询 in、not in、=、!=、exists、not exists


(7)联合 union、union all 区别在于union会去掉重复的记录

3.DCL 数据库控制语句
grant 增删改查的权限以逗号分隔 on 数据库.* to '用户名'@'数据库安装在哪里' identified by '密码'
revoke 权限名 on 数据库.* from '用户名'@'数据库安装在哪里'

posted @ 2017-07-12 17:23  大都比2号  阅读(666)  评论(0编辑  收藏  举报