MySQL 相关

一、 DDL

1. 数据库操作

(1)查询

  • 查询所有数据库

    1
    show databases;
  • 查询当前数据库

    1
    select database();

(2)创建

  • 创建数据库

    1
    create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];

(3)删除

  • 删除数据库

    1
    drop database [if exists] 数据库名

(4)使用

  • 使用数据库

    1
    use 数据库名;

2. 表操作

(1)查询 show | desc

  • 查询当前数据库所有表

    1
    show tables;
  • 查询表结构

    1
    2
    3
    desc 表名; 
    describe 表名;
    show columns from 表名;
  • 查询指定表的建表语句

    1
    show create table 表名;

(2)创建 create table | create index

  • 建表语句

    1
    create table 表名 (列名1 数据类型 [约束] [comment 注释], 列名2 数据类型 [约束] [comment 注释], ...);
  • 创建索引

    1
    create [unique|fulltext|spatial] index 索引名称 on 表名(字段名称[(长度)] [asc|desc])

(3)修改 alter table

  • 添加字段

    1
    2
    alter table 表名 
    add [column] 字段名 数据类型 [约束] [comment 注释];
  • 添加索引

    1
    2
    alter table 表名 
    add [unique|fulltext|spatial] index 索引名称(字段名称[(长度)] [asc|desc]);
  • 添加 check 约束(域完整性)

    1
    2
    alter table 表名
    add constraint 约束名称 check (条件);
  • 添加唯一约束(实体完整性)

    1
    2
    3
    # 唯一约束
    alter table 表名
    add constraint [约束名称(若不设置,则以字段名称作为约束名称)] unique (字段名称);
  • 添加主键约束和外键约束(参照完整性)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 主键约束
    alter table 表名
    add constraint [约束名称(在 MySql 即使设置该参数也会被忽略)] primary key (字段名称);

    # 外键约束
    alter table 表名
    add constraint 约束名称 foreign key(字段名称)
    references 被引用表名 (被引用字段名称);
    # 注意:必须先为被引用字段创建索引,通常是主键索引
  • 修改数据类型

    1
    2
    alter table 
    modify 字段名 新数据类型 [约束] [comment 注释];
  • 修改字段名和数据类型

    1
    2
    alter table 
    change 旧字段名 新字段名 新数据类型 [约束] [comment 注释];
  • 修改表名

    1
    2
    3
    alter table 旧表名 rename to 新表名;
    # 或者
    rename table 旧表名 to 新表名;

(4)删除 drop table | truncate table | alter table drop | drop index

  • 删除表

    1
    drop table [if exists] 表名;
  • 删除,并重建指定表

    1
    truncate table 表名;
  • 删除字段

    1
    2
    alter table 表名 
    drop 字段名;
  • 删除约束

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    alter table 表名
    drop constraint 约束名;

    # 删除主键约束有三种写法,分别是:
    alter table 表名
    drop primary key;
    # 或者 (不推荐)
    alter table 表名
    drop constraint `primary`;
    # 或者 (不推荐)
    drop index `primary` on 表名;
  • 删除索引

    1
    2
    3
    4
    alter table 表名
    drop index 索引名;
    # 或者
    drop index 索引名 on 表名;

(5)常用的数据类型

① 数值类型

类型大小范围(有符号)范围(无符号)用途
tinyint1 Bytes(-128,127)(0,255)小整数值
smallint2 Bytes(-32 768,32 767)(0,65 535)大整数值
mediumint3 Bytes(-8 388 608,8 388 607)(0,16 777 215)大整数值
int integer4 Bytes(-2 147 483 648,2 147 483 647)(0,4 294 967 295)大整数值
bigint8 Bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)极大整数值
float4 Bytes(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)0,(1.175 494 351 E-38,3.402 823 466 E+38)单精度 浮点数值
double8 Bytes(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)双精度 浮点数值
decimal对decimal(M,D) ,如果M>D,为M+2否则为D+2依赖于M和D的值依赖于M和D的值小数值

② 日期类型

类型大小(Bytes)范围格式用途
date31000-01-01/9999-12-31YYYY-MM-DD日期值
time3‘-838:59:59’/‘838:59:59’HH:MM:SS时间值或持续时间
year11901/2155YYYY年份值
datatime8‘1000-01-01 00:00:00’ 到 ‘9999-12-31 23:59:59’YYYY-MM-DD hh:mm:ss混合日期和时间值
timestamp4‘1970-01-01 00:00:01’ UTC 到 ‘2038-01-19 03:14:07’ UTC结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07YYYY-MM-DD hh:mm:ss混合日期和时间值,时间戳

③ 字符串类型

char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。

类型大小用途
char0-255 bytes定长字符串
varchar0-65535 bytes变长字符串
tinyblob0-255 bytes不超过 255 个字符的二进制字符串
tinytext0-255 bytes短文本字符串
blob0-65 535 bytes二进制形式的长文本数据
text0-65 535 bytes长文本数据
mediumblob0-16 777 215 bytes二进制形式的中等长度文本数据
mdeiumtext0-16 777 215 bytes中等长度文本数据
longblob0-4 294 967 295 bytes二进制形式的极大文本数据
longtext0-4 294 967 295 bytes极大文本数据

3. 视图操作

(1)查询

  • 查询所有视图

    1
    2
    show tables;  -- 视图和表一起显示
    show full tables where table_type = 'VIEW'; -- 只显示视图
  • 查询视图结构

    1
    2
    desc 视图名;
    show columns from 视图名;
  • 查询视图创建语句

    1
    show create view 视图名;

(2)创建

  • 创建视图

    1
    2
    3
    4
    create [or replace] [algorithm = {undefined | merge | temptable}]
    view 视图名 [(列名列表)]
    as select语句
    [with [cascaded | local] check option];
  • 简化语法

    1
    create view 视图名 as select语句;

(3)修改

  • 修改视图

    1
    2
    3
    alter view 视图名 [(列名列表)] as select语句;
    # 或者
    create or replace view 视图名 as select语句;

(4)删除

  • 删除视图

    1
    drop view [if exists] 视图名1 [, 视图名2, ...];

二、DML

1. 插入数据 insert

(1)给指定字段添加数据

1
insert into 表名 (字段名1, 字段名2, ...) values (值1, 值2, ...);

(2)给全部字段添加数据

1
insert into 表名 values (值1, 值2, ...);

(3)批量添加数据

1
2
3
4
5
6
7
8
9
10
11
# 指定字段批量插入
insert into 表名 (字段名1, 字段名2, ...) values
(值1, 值2, ...),
(值1, 值2, ...),
(值1, 值2, ...);

# 全字段批量插入
insert into 表名 values
(值1, 值2, ...),
(值1, 值2, ...),
(值1, 值2, ...);

(4)插入查询结果

1
2
insert into 表名 (字段名1, 字段名2, ...) 
select 字段名1, 字段名2, ... from 源表名 [where 条件];

2. 修改数据 update

(1)修改数据

1
update 表名 set 字段名1 = 值1, 字段名2 = 值2, ... [where 条件];

(2)关联修改

1
2
3
update 表1, 表2 
set 表1.字段名 = 值, 表2.字段名 = 值
where 关联条件 [and 其他条件];

3. 删除数据 delete

(1)删除数据

1
delete from 表名 [where 条件];

(2)关联删除

1
delete 表1, 表2 from 表1, 表2 where 关联条件 [and 其他条件];

(3)删除与清空的区别

命令特点
delete逐行删除,可回滚,不重置自增列,速度较慢
truncate直接删除表并重建,不可回滚,重置自增列,速度快

三、 DQL

1. 基础查询

(1)查询多个字段

1
2
select 字段1, 字段2, ... from 表名;
select * from 表名; -- 查询所有字段

(2)设置别名

1
select 字段1 [as 别名1], 字段2 [as 别名2] from 表名;

(3)去除重复记录

1
select distinct 字段列表 from 表名;

2. 条件查询 where

(1)比较运算符

运算符功能
>大于
>=大于等于
<小于
<=小于等于
=等于
<> !=不等于
between...and...在某个范围之内(含最小、最大值)
in(...)在in之后的列表中的值,多选一
like 占位符模糊匹配(_匹配单个字符,%匹配任意个字符)
is null是null

(2)逻辑运算符

运算符功能
and并且(多个条件同时成立)
or或者(多个条件任意一个成立)
not非,不是

(3)条件查询语法

1
select 字段列表 from 表名 where 条件列表;

3. 聚合函数 count | max | min | avg | sum

(1)常见聚合函数

函数功能
count统计数量
max最大值
min最小值
avg平均值
sum求和

(2)聚合函数语法

1
select 聚合函数(字段列表) from 表名;

注意:null值不参与所有聚合函数运算

4. 分组查询 group by

(1)语法

1
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];

(2)where 与 having 区别

执行时机支持的函数
where分组之前进行过滤,不满足where条件,不参与分组不能使用聚合函数
having分组之后对结果进行过滤可以使用聚合函数

5. 排序查询 order by

(1)语法

1
select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2;

(2)排序方式

  • asc:升序(默认值)
  • desc:降序

注意:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序

6. 分页查询 limit

(1)语法

1
select 字段列表 from 表名 limit 起始索引, 查询记录数;

注意:起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数

7. 连接查询

(1)内连接

  • 隐式内连接

    1
    select 字段列表 from 表1, 表2 where 条件...;
  • 显式内连接

    1
    select 字段列表 from 表1 [inner] join 表2 on 连接条件...;

(2)外连接

  • 左外连接

    1
    select 字段列表 from 表1 left [outer] join 表2 on 条件...;
  • 右外连接

    1
    select 字段列表 from 表1 right [outer] join 表2 on 条件...;

(3)自连接

1
select 字段列表 from 表a 别名a join 表a 别名b on 条件...;

8. 子查询

(1)标量子查询

子查询结果为单个值

1
select * from 表1 where 字段 = (select 字段 from 表2 where 条件);

(2)列子查询

子查询结果为一列

操作符描述
in在指定的集合范围之内,多选一
not in不在指定的集合范围之内
any子查询返回列表中,有任意一个满足即可
some与any等同,使用some的地方都可以使用any
all子查询返回列表的所有值都必须满足
1
select * from 表1 where 字段 in (select 字段 from 表2 where 条件);

(3)行子查询

子查询结果为一行

1
select * from 表1 where (字段1, 字段2) = (select 字段1, 字段2 from 表2 where 条件);

(4)表子查询

子查询结果为多行多列

1
select * from 表1 where (字段1, 字段2) in (select 字段1, 字段2 from 表2 where 条件);

9. 多表查询案例

(1)查询语法顺序

1
2
3
4
5
6
7
select 字段列表
from 表名列表
where 条件列表
group by 分组字段列表
having 分组后条件列表
order by 排序字段列表
limit 分页参数;

(2)执行顺序

  1. from 表名列表
  2. where 条件列表
  3. group by 分组字段列表
  4. having 分组后条件列表
  5. select 字段列表
  6. order by 排序字段列表
  7. limit 分页参数

四、 DCL

1. 用户管理

(1)查询用户

1
2
use mysql;
select * from user;

(2)创建用户

1
create user '用户名'@'主机名' identified by '密码';

(3)修改用户密码

1
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';

(4)删除用户

1
drop user '用户名'@'主机名';

(5)主机名说明

主机名含义
localhost当前主机
%任意主机
192.168.1.100指定主机

2. 权限控制

(1)查询权限

1
show grants for '用户名'@'主机名';

(2)授予权限

1
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';

(3)撤销权限

1
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';

(4)常用权限

权限说明
all, all privileges所有权限
select查询数据
insert插入数据
update修改数据
delete删除数据
alter修改表
drop删除数据库/表/视图
create创建数据库/表/视图

(5)权限控制案例

1
2
3
4
5
6
7
8
9
10
11
12
# 创建用户并授权
create user 'test'@'localhost' identified by '123456';
grant select, insert, update on test_db.* to 'test'@'localhost';

# 查看权限
show grants for 'test'@'localhost';

# 撤销权限
revoke insert, update on test_db.* from 'test'@'localhost';

# 删除用户
drop user 'test'@'localhost';

3. 常用系统函数

(1)字符串函数

函数功能
concat(s1,s2,...,sn)字符串拼接
lower(str)转小写
upper(str)转大写
lpad(str,n,pad)左填充,用字符串pad对str的左边进行填充,达到n个字符串长度
rpad(str,n,pad)右填充,用字符串pad对str的右边进行填充,达到n个字符串长度
trim(str)去掉字符串头部和尾部的空格
substring(str,start,len)返回从字符串str从start位置起的len个长度的字符串

(2)数值函数

函数功能
ceil(x)向上取整
floor(x)向下取整
mod(x,y)返回x/y的模
rand()返回0~1内的随机数
round(x,y)求参数x的四舍五入的值,保留y位小数

(3)日期函数

函数功能
curdate()返回当前日期
curtime()返回当前时间
now()返回当前日期和时间
year(date)获取指定date的年份
month(date)获取指定date的月份
day(date)获取指定date的日期
date_add(date, interval expr type)返回一个日期/时间值加上一个时间间隔expr后的时间值
datediff(date1, date2)返回起始时间date1和结束时间date2之间的天数

(4)流程函数

函数功能
if(value,t,f)如果value为true,则返回t,否则返回f
ifnull(value1,value2)如果value1不为空,返回value1,否则返回value2
case when [val1] then [res1] ... else [default] end如果val1为true,返回res1,…否则返回default默认值
case [expr] when [val1] then [res1] ... else [default] end如果expr的值等于val1,返回res1,…否则返回default默认值