MySQL基础应用
SQL介绍
1 | 结构化查询语言 |
常用SQL分类
1 | DDL:数据定义语言 |
数据类型、表属性、字符集
数据类型
作用
保证数据的准确性和标准性
种类
- 数值类型
1
2
3tinyint : -128~127
int :-2^31~2^31-1
说明:手机号是无法存储到int的。一般是使用char类型来存储手机号 - 字符类型
1
2
3
4
5
6char(11) :
定长 的字符串类型,在存储字符串时,最大字符长度11个,立即分配11个字符长度的存储空间,如果存不满,空格填充。
varchar(11):
变长的字符串类型看,最大字符长度11个。在存储字符串时,自动判断字符长度,按需分配存储空间。
enum('bj','tj','sh'):
枚举类型,比较适合于将来此列的值是固定范围内的特点,可以使用enum,可以很大程度的优化我们的索引结构。 - 时间类型
1
2
3
4datatime
范围为从 1000-01-01 00:00:00.000000 至 9999-12-31 23:59:59.999999
timestamp
范围为从 1970-01-01 00:00:00.000000 至 2038-01-19 03:14:07.999999- 二进制类型
- 二进制类型
- 数值类型
列属性
1 | 约束(一般建表时添加): |
表属性
1 | 存储引擎: |
字符集
1 | show charset; # 查看 mysql 支持的所有字符集 |
DDL的应用
库定义
创建数据库
1
2
3
4
5
6
7
8建库规范:
1.库名不能有大写字母
2.建库要加字符集
3.库名不能有数字开头
4. 库名要和业务相关
建库标准语句
create database test charset utf8mb4 collate utf8mb4_bin;删除
1
2# 生产中禁止
drop database school;修改
1
2
3
4
5
6
7
8
9# 创建一个没有设置字符集的库
create database school;
# 如果是已经创建好的库的查看建库语句
show create database school;
# 修改字符集
alter database school charset utf8mb4 collate utf8mb4_bin;
# 注意:修改字符集,修改后的字符集一定是原字符集的严格超集(从小往大,不能从大往小)查询(属于DQL)
1
2
3
4
5# 查看所有库
show databases;
# 查看库的建库语句
show create database test;
表定义
创建表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30建表规范:
1. 表名小写
2. 不能是数字开头
3. 注意字符集和存储引擎
4. 表名和业务有关
5. 选择合适的数据类型
6. 每个列都要有注释
7. 每个列设置为非空,无法保证非空,用0来填充
格式:
create table stu(
列1 属性(数据类型、约束、其他属性) ,
列2 属性,
列3 属性
)
创建一个学生表:
use school;
create table stu(
id int not null primary key auto_increment comment '学号',
sname varchar(255) not null comment '姓名',
sage tinyint unsigned not null default 0 comment '年龄',
sgender enum('m','f','n') not null default 'n' comment '性别',
sfz char(18) not null unique comment '身份证',
intime timestamp not null default now() comment '入学时间'
) engine=innodb charset= utf8mb4 comment '学生表';
show tables; # 查看是否创建成功
desc stu; # 查看表结构信息
show create table stu; # 查看建表语句修改表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19# 在stu表中添加qq列
alter table stu add qq varchar(20) not null unique comment 'qq号';
# 在sname后加微信列
alter table stu add wechat varchar(20) not null unique comment '微信号' after sname;
# 在id列前面加一个新列的num
alter table stu add num int not null comment '数字' first;
# 修改sname数据类型
alter table stu modify sname varchar(128) not null;
# 将sgender该为sex 数据类型改为char类型
alter table stu change sgender sex char(1) not null default 'n';
# 删除刚才添加的列
alter table stu drop num;
alter table stu drop qq;
alter table stu drop wechat;查询表属性(DQL)
1
2
3
4
5use school;
show tables;
desc stu;
show create table stu;
create table test like stu; # 创建一个和stu表结构一样的test表删除表
1
drop table stu;
DCL的应用
1 | 用户的授权和权限的回收 |
DML的应用
对表中数据的增删改
insert
1 | # 标准插入语句 |
update
1 | select * from stu; |
delete
1 | delete from stu where id=9; |
DQL的应用(select
)
作用: 获取MySQL中的数据行
单独使用
1 | -- select @@xxx 查看系统参数 |
1 | -- select 函数(); |
单表查询
SQL92标准的使用语法
select语法执行顺序
1 | select开始 ----> from子句 ----> where子句 ----> group by子句 ----> select后执行条件 ----> having子句 ----> order by子句 ----> limit |
单表环境准备
1 | # 如果下面的失效了在该网站下载, https://dev.mysql.com/doc/index-other.html |
FROM
1 | use world; |
WHERE
1 | # where 配合 等值查询(=) |
group by
配合聚合函数应用
根据 by后面的条件进行分组,方便统计,by后面跟一个列或多个列
常用聚合函数
1
2
3
4
5
6max() # 最大值
min() # 最小值
avg() # 平均值
sum() # 总和
count() # 个数
group_concat() # 列转行
1 | # 统计每个国家的总人口 |
having
1 | # 统计中国每个省的总人口数,只打印总人口数小于1000000的省 |
order by
实现先排序, by后添加条件列
1 | # 统计中国每个省的总人口数并从小到大排序 |
limit
1 | # 统计中国每个省的总人口数并从大到小排序只显示前5个 |
多表连接查询
将来要查询的数据,是来自于多张表时,可以用多表连接
环境准备
1 | 表结构: |
- 表之间的关系
内连接
1 | # 查询人口数量小于100人的国家名,城市名,国土面积 |
1 | # 查询oldguo老师教的学生姓名列表 |
别名
1 | # 表别名设置 |
外连接
左外连接和右外连接
1 | # 内连接 |
information_schema.tables
视图
介绍
1 | 1.虚拟库 |
作用
1 | 此库中的视图是用来,间接的查询数据库的"元数据"("基表"数据) |
1 | # 记录了:整个MySQL数据库中,所有的表的详细属性信息 |
CONCAT()
函数使用
1 | # 简单使用 |
相关使用
查询整个数据库中所有库和所对应的表信息
1
2
3select table_schema,group_concat(table_name)
from information_schema.tables
group by table_schema;统计所有库下 的表个数
1
2
3select table_schema,count(table_name)
from information_schema.tables
group by table_schema;查询所有innodb引擎的表及所在的库
1
2
3select table_schema,table_name,engine
from information_schema.tables
where engine='innodb';统计world的数据库下每个表的行数
1
2
3select table_name,table_rows
from information_schema.tables
where table_schema='world';统计world数据库下每张表的磁盘空间占用
1
2
3select table_name,concat((TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") as size_KB
from information_schema.tables
where table_schema='world';统计所有数据库的总磁盘空间占用
1
2
3
4
5select table_schema,concat(sum(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") as Total_KB
from information_schema.tables
group by table_schema;
mysql -uroot -p1 -e 'select table_schema,concat(sum(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") as Total_KB from information_schema.tables group by table_schema;'生成整个数据库下的所有表的单独备份语句
1
2
3
4
5
6
7
8
9vim /etc/my.cnf
secure-file-priv=/tmp # 在mysqld 标签下添加
systemctl restart mysqld.service # 重启数据库
模板语句:
mysqldump -uroot -p123 world city >/tmp/world_city.sql
批量生成整个数据库的所有表的备份语句:
select concat("mysqldump -uroot -p1 ",table_schema," ",table_name," > /bak/",table_schema,"_",table_name,".sql") from information_schema.tables;
常用show语句
1 | show databases; # 查看所有数据库 |