MySQL 基本操作
- 数据库
show databases; # 显示所有数据库
create datebase 数据库 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; # 创建数据库
use 数据库; # 使用数据库
show tables; # 显示当前数据库所有的表
show create database 数据库; # 显示创建该库的语句
drop database 数据库; # 删除数据库
- 数据表
create table '数据表' ('字段1' 类型 属性,'字段2' 类型 属性,'字段3' 类型 属性,···)ENGINE=InnoDB DEFAULT CHARSET=utf8; # 创建数据表
show create table 数据表; # 显示创建该表的语句
drop table 数据表; # 删除表
desc 数据表; 查看表的字段
- 表数据的基本操作
# 增加数据
insert into 数据表 values(值1, 值2, 值2,); # 插入整条数据
insert into 数据表(字段1, 字段2, 字段3,) values(值1, 值2, 值3); # 插入部分数据
insert into 数据表 values(数据1), (数据2)···; # 插入多行数据
# 删除数据
delete from 数据表 where 条件;
# 修改数据
update 数据表 set 更新条件 where 查找条件;
# 查找数据
select * from 数据表 where 条件1 and 条件2···;
- 表数据的查询操作(由于查询操作有很多复杂的操作,所以单独抽出)
- 单表查询
# 查询表中所有数据
select * from 数据表;
# 查询指定字段
select 字段1, 字段2 ··· from 数据表;
# 给字段起别名(as)
select 字段1 as 别名1, 字段2 as 别名2 from 数据表;
# 给表起别名
select 表别名.字段1, 表别名.字段2 from 数据表 as 表别名;
# 消除重复行(distinct)
select distinct 字段名 from 数据表;
# 判断是否为空(is null)
select * from 数据表 where 条件 is null;
# 排序(order by(desc: 倒叙, asc: 顺序))
select * from 数据表 where 条件 order by 字段;
select * from 数据表 where 条件 order by 字段 desc/asc;
select * from 数据表 where 条件 order by 字段1 desc, 字段2 asc; # 以字段1倒叙且以字段2顺序
# 分组(group by)
select 字段 from 数据表 group by 字段
select 字段 from 数据表 group by 字段 having 条件
# 分页(limit)
select * from 数据表 where 条件 limit 起始, 终止
# 条件查询(where)
select * from 数据表 where 条件; # 单条件查询
# 逻辑运算(and, or)
select * from 数据表 where 条件1 and 条件2 ···;
select * from 数据表 where 条件1 or 条件2 ···;
# 比较运算(>, <, !=)
select * from 数据表 where 条件 > 值;
select * from 数据表 where 条件 < 值;
select * from 数据表 where 条件 != 值;
# 模糊查询(like)
select * from 数据表 where 条件 like "值%"; # 以'值'开头的数据
select * from 数据表 where 条件 like "%值"; # 以'值'结尾的数据
select * from 数据表 where 条件 like "%值%"; # 包含'值'的数据
select * from 数据表 where 条件 like "__"; # 条件是两个字
select * from 数据表 where 条件 like "_值_"; # 条件中包含'值'且前后各有一个字
select * from 数据表 where 条件 like "%__%"; # 条件至少两个字
- 多表查询
# 内连接(inner join)
select 表1.字段, 表2.字段 from 表1 inner join 表2 on 表1的外键 = 表2的主键;
# 左连接(left join)
select 表1.字段, 表2.字段 from 表1 left join 表2 on 表1的外键 = 表2的主键;
# 右连接(right join)
select 表1.字段, 表2.字段 from 表1 right join 表2 on 表1的外键 = 表2的主键;
- 子查询
# 标准子查询
select * from 表1 where 字段1 = (select 字段1 from 表2 where 条件)
# 行级子查询
select * from 表1 where (字段1, 字段2) = (select 字段1, 字段2 from 表2 where 条件)
# 列级子查询
select * from 表1 where 字段1 in (select 字段1 from 表2 where 条件)
- 聚合函数
# 统计(count)
select count(*) from 数据表 where 条件;
# 运算(max, min, avg, sum)
select max(字段), min(字段), avg(字段), sum(字段) from 数据表 where 条件;
- 视图
# 创建视图
create view 视图名 as select 表1.字段, 表2.字段 from 表1 inner join 表2 on 表1外键 = 表2主键;
# 删除视图
drop view 视图名;
- 事务
# 开启事务
begin;
start transaction;
# 提交事务
commit;
# 事务回滚
rollback;
- 索引
# 查看索引
show index from 数据表;
# 创建索引
create index 索引名 on 数据表;
# 删除索引
drop index 索引名 on 数据表;
- 用户权限
# 授予用户权限
grant select, update, insert, delete··· on 数据库.数据表 to 用户名@主机;
# 删除用户权限
revoke selwct, update, insert, delete··· on 数据库.数据表 from 用户名@主机;
# 刷新权限信息
flush privileges;
# 查看用户权限
show grants for 用户名@主机;
# 删除用户
drop user 用户名@主机;