概述
database > table (column | row)
Mysql
schemata: 列出所有数据库
tables: 列出所有表及其属性
columns: 列出所有列及其属性
statistics: 列出索引信息
user_privileges 和 schema_privileges: 列出用户权限
select schema_name from information_schema.schemata;
select table_name from information_schema.tables;
select * from information_schema.tables where table_name='emp';
select column_name from information_schema.columns;
select column_name from information_schema.columns where table_name='emp';
use information_schema
show tables;
select * from tables;
select * from columns;
SQL
SQL通用语法
SQL 分类
数据类型
数据类型
| 类型 |
bytes |
有符号范围(signed) |
无符号(unsigned)范围 |
描述 |
| tinyint |
1 |
(-128, 127) |
(0, 255) |
小整数值 |
| smallint |
2 |
(-32768, 32767) |
(0, 65535) |
大整数值 |
| mediumint |
3 |
(-8388608, 8388607) |
(0, 16777215) |
大整数值 |
| int/integer |
4 |
(-2147483648, 2147483647) |
(0, 4294967295) |
大整数值 |
| bigint |
8 |
(-2^63, 2^63-1) |
(0, 2^64-1) |
极大整数值 |
| float |
4 |
(-3.402823466 E+38, 3.402823466351 E+38) |
0 和 (1.175494351 E-38, 3.402823466 E+38) |
单精度浮点数值 |
| double |
8 |
(-1.7976931348623157 E+308, 1.7976931348623157 E+308) |
0 和 (2.2250738585072014 E-308, 1.7976931348623157 E+308) |
双精度浮点数值 |
| decimal |
|
依赖于 M (精度) 和 D (标度) 的值 |
依赖于 M (精度) 和 D (标度) 的值 |
小数值(精确点数) |
字符类型
| 类型 |
大小 bytes |
描述 |
| char |
0-255 |
定长字符串(需要指定长度) |
| varchar |
0-65535 |
变长字符串(需要指定长度) |
| tinyblob |
0-255 |
不超过255个字符的二进制数据 |
| tinytext |
0-255 |
短文本字符串 |
| blob |
0-65535 |
二进制形式的长文本数据 |
| text |
0-65535 |
长文本数据 |
| mediumblob |
0-16777215 |
二进制形式的中等长度文本数据 |
| mediumtext |
0-16777215 |
中等长度文本数据 |
| longblob |
0-4294967295 |
二进制形式的极大文本数据 |
日期时间类型
| 类型 |
bytes |
范围 |
格式 |
描述 |
| date |
3 |
1000-01-01 至 9999-12-31 |
YYYY-MM-DD |
日期值 |
| time |
3 |
-838:59:59 至 838:59:59 |
HH:MM:SS |
时间值或者持续时间 |
| year |
1 |
1901 至 2155 |
YYYY |
年份值 |
| datetime |
8 |
1000-01-01 00:00:00 至 9999-12-31 23:59:59 |
YYYY-MM-DD HH:MM:SS |
混合日期和时间值 |
| timestamp |
4 |
1970-01-01 00:00:01 值 2038-01-19 03:14:07 |
YYYY-MM-DD HH:MM:SS |
混合日期和时间值,时间戳 |
其他
权限
| 权限 |
说明 |
| ALL,ALL PRIVILEGES |
所有权限 |
| select |
查询数据 |
| insert |
插入数据 |
| update |
修改数据 |
| delete |
删除数据 |
| alter |
修改表 |
| drop |
删除数据库/表/视图 |
| create |
创建数据库/表 |
运算符
| 运算符 |
描述 |
| > |
大于 |
| >= |
大于等于 |
| < |
小于 |
| <= |
小于等于 |
| = |
等于 |
| <> 或者 != |
不等于 |
| between … and … |
在某个范围之内(含最小,最大值) |
| in (…) |
在 in 之后的列表中的值,多旋一 |
| like 占位符 |
模糊匹配 (_匹配单个字符,%匹配任意字符) |
| is NULL |
是 NULL |
逻辑运算符
| 逻辑运算符 |
功能 |
| AND 或 && |
并且(多个条件都成立) |
| OR 或 || |
或者(多个条件任意一个成立) |
| NOT 或 ! |
非,不是 |
聚合函数
将一列数据作为一个整体,进行纵向计算。
| 聚合函数 |
功能 |
| count |
统计数量 |
| max |
最大值 |
| min |
最小值 |
| avg |
平均值 |
| sum |
求和 |
安装
Docker 安装 MySQL5.7
安装命令
docker run -d -p 3306:3306 --privileged=true \
-v /home/hjx/docker/mysql/conf/my.cnf:/etc/my.cnf \
-v /home/hjx/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=hadoop \
--name mysql mysql:5.7 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci
language-shell复制代码
创建用户
# 先进入容器
docker exec -it mysql bash
# 执行MySQL命令, 输入root密码, 连接MySQL
mysql -uroot -p
# 输入密码后, 执行下面命令创建新用户 (用户名: test , 密码: test123)
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY 'test123' WITH GRANT OPTION;
language-shell复制代码
Docker 安装 MySQL8
- 拉起镜像
docker pull mysql:8.0.12
language-shell复制代码
- 创建数据目录和配置文件
mkdir -p /usr/mysql/conf /usr/mysql/data
chmod -R 755 /usr/mysql/
language-shell复制代码
- 创建配置文件
vim /usr/mysql/conf/my.cnf
[client]
default-character-set = utf8mb4
[mysqld]
datadir = /usr/mysql/data
character_set_server = utf8mb4
collation_server = utf8mb4_bin
secure-file-priv= NULL
symbolic-links=0
- 创建容器
docker run --restart=unless-stopped -d --name mysql8 \
-v /usr/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/mysql/data:/var/lib/mysql \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=hadoop mysql
language-shell复制代码
- 修改mysql密码以及可访问主机
use mysql
# 修改访问主机以及密码等,设置为所有主机可访问
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '新密码';
# 刷新
flush privileges;
language-shell复制代码
靶场环境
phpStudy
在path中添加:D:\phpstudy_pro\Extensions\MySQL5.7.26\bin
mysql -u root -p
mysql -h 192.168.16.2 -P 3396 -p
使用
快速使用
database
- 查看库
show databases;
- 创建库
create database db_name;
- 删除库
drop database db_name;
- 选择库
use db_name;
- 查看表
show tables;
- 查看创建库创建语句
show create database db_name;
- 查看选中的数据库
select database();
- 修改数据库字符集
alter database db_name default charset=utf8;
table
- 查看表结构
desc table_name;
- 查看创建表的语句
show create table table_name;
- 创建表
create table table_name(
字段1 字段类型 comment '字段1的注释',
字段2 字段类型 comment '字段2的注释',
...
) comment '表注释'
create table test(
id int comment '编号',
name varchar(50) comment '姓名',
age int comment '年龄',
gender varchar(1) comment '性别'
) comment '用户表';
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
entrydate date comment '入职时间'
) comment '员工表';
- 修改表
alter table emp add nickname varchar(20) comment '昵称';
alter table emp modify nickname varchar(30);
alter table emp change nickname username varchar(50) comment '用户名';
alter table emp drop username;
alter table emp rename to employee;
- 删除表
drop table employee;
Data
create table employee(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
entrydate date comment '入职时间'
) comment '员工表';
- 插入数据
insert into employee (id, workno, name, gender, age, idcard, entrydate) values (1,'001','mz','男',18,'000000000000110000','2025-09-09');
insert into employee values (2,'002','mzz','女',19,'000000000000120000','2025-09-10');
insert into employee values
(3,'003','mzz3','女',19,'000000000000130000','2025-09-13'),
(4,'004','mzz4','女',19,'000000000000140000','2025-09-14'),
(5,'005','mzz5','女',19,'000000000000150000','2025-09-15');
- 修改数据
update employee set name='mzz6' where id=4;
update employee set id=3,name='mzz3',gender='男' where id=5;
update employee set name='mzz6' where id=5 and
- 删除数据
delete from employee where id=3;
delete from employee;
user
- 查看用户
select * from mysql.user;
select user,host from mysql.user;
select user();
- 创建用户
create user 'mz'@'localhost' identified by '123456';
- 修改密码
alter user 'mz'@'localhost' identified with mysql_native_password by '123';
- 删除用户
drop user 'mz'@'localhost';
权限
| 权限 |
说明 |
| ALL,ALL PRIVILEGES |
所有权限 |
| select |
查询数据 |
| insert |
插入数据 |
| update |
修改数据 |
| delete |
删除数据 |
| alter |
修改表 |
| drop |
删除数据库/表/视图 |
| create |
创建数据库/表 |
- 查看权限
show grants for '用户名'@'主机名';
show grants for 'mz'@'localhost';
use information_schema;
show tables;
select * from tables;
show grants for 'root'@'localhost';
- 添加权限
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
grant select on mz.test to 'mz'@'localhost';
grant select on mz.* to 'mz'@'localhost';
grant create,drop,alter on mz.* to 'mz'@'localhost';
grant all on mz.* to 'mz'@'localhost';
- 撤销权限
revoke 权限列表 on 数据库名.表名 to '用户名'@'主机名';
revoke select no mz.* from 'mz'@'localhost';
修改host登录
update user set host='ip' where user='root';
update user set host='%' where user='root';
alter user 'root'@'ip' identified with mysql_native_password by 'root'
flush privileges;
查询
select
字段列表
from
表名列表
where
条件列表
group by
分组字段列表
having
分组后条件列表
order by
排序字段列表
limit
分页参数
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
)comment '员工表';
INSERT INTO emp VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01'),(2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01'),(3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01'),(4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01'),(5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01'),(6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01'),(7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01'),(8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01'),(9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01'),(10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01'),(11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01'),(12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01'),(13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01'),(14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01'),(15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01'),(16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');
基础查询 select
select * from emp;
select name,gender from emp;
select name as 姓名, gender as 性别 from emp;
select distinct gender from emp;
select name,workno,age from emp;
select * from emp;
select workaddress as '工作地址' from emp;
select distinct workaddress from emp;
聚合函数 count
将一列数据作为一个整体,进行纵向计算。
| 聚合函数 |
功能 |
| count |
统计数量 |
| max |
最大值 |
| min |
最小值 |
| avg |
平均值 |
| sum |
求和 |
select count(*) from emp;
select count(*) as total from emp;
select count(idcard) as total from emp;
select avg(age) from emp;
select max(age) from emp;
select min(age) from emp;
select sum(age) from emp;
select avg(age) from emp where workaddress='西安';
条件查询 where
| 运算符 |
描述 |
| > |
大于 |
| >= |
大于等于 |
| < |
小于 |
| <= |
小于等于 |
| = |
等于 |
| <> 或者 != |
不等于 |
| between … and … |
在某个范围之内(含最小,最大值) |
| in (…) |
在 in 之后的列表中的值,多旋一 |
| like 占位符 |
模糊匹配 (_匹配单个字符,%匹配任意字符) |
| is NULL |
是 NULL |
| 逻辑运算符 |
功能 |
| AND 或 && |
并且(多个条件都成立) |
| OR 或 || |
或者(多个条件任意一个成立) |
| NOT 或 ! |
非,不是 |
select * from emp where age=18;
select * from emp where age<20;
select * from emp where age<=20;
select * from emp where idcard is null;
select * from emp where idcard is not null;
select * from emp where age!=88;
select * from emp where age<>88;
select * from emp where age >= 15 and age <=20;
select * from emp where age between 15 and 20;
select * from emp where gender='女' and age<25;
select * from emp where age=18 or age=20 or age=40;
select * from emp where age in(18,20,40);
select * from emp where name like '__';
select * from emp where idcard like '_________________X';
select * from emp where idcard list '%X';
分组查询 group by
select gender,count(*) from emp group by gender;
select gender,count(*) total from emp group by gender;
select gender,avg(age) from emp group by gender;
select workaddress,count(*) total from emp where age < 45 group by workaddress having total >= 3;
select workaddress,gender, count(*) 'total' from emp group by workaddress,gender;
排序查询 order by (降序desc)
select * from emp order by age;
select * from emp order by age asc;
select * from emp order by age desc;
select * from emp order by entrydate desc, age asc;
分页查询 limit
select * from emp limit 0,10;
select * from emp limit 10,20;
联合查询 union
select * from emp where id < 10
union
select * from emp where age = 88;
select * from emp where id < 10
union all
select * from emp where age = 88;
其他
MySQL 触发器
MySQL 数据库引擎
MySQL 事务
MySQL 分区分表
MySQL 视图
MySQL 数据库设计及SQL优化
MySQL 数据备份还原
SQL 注入
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 hjxstart@126.com