ParisGabriel
每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰
开局一张图
表字段重命名 (change)
alter table 表名 change 原名 新名 数据类型;
SQL 查询
执行顺序:
3. select ... 聚合函数 from 表名
1. where ...
2. group by...
4. having...
5. order by...
6. limit...
group by 语句
作用: 给查询结果进行分组
注意:
1.group by 之后的字段必须要为 select 之后的字段名
2. 如果 select 之后的字段和 group by 之后的字段不一致, 则必须对
字段进行聚合处理 (聚合函数)
having 语句
作用: 对查询结果进一步筛选
注意:
1.having 语句通常和 group by 联合使用, 过滤由 group by 语句返回的记录集
2.where 只能操作表中实际存在的字段, having by 可操作由聚合函数生成的显示列
distinct
作用: 不显示字段重复值
注意:
1.distinct 和 from 之间所有字段都相同 才会去重
2.distinct 不能对任何字段做聚合处理
运算符:
- + - * / %
- ## sudo apt-get install python3-pip 安装 pip3
- ## sudo pip3 install pymysql 安装 mysql
约束:
1. 作用: 保证数据的完整性, 一致性, 有效性
2. 约束分类
1. 默认约束 (default)
插入字段时不给该字段赋值 则使用默认值
2. 非空约束 (not NULL)
不允许该字段值有 NULL 记录
sex enum("M","F","S") not null defaulf "S"
索引
定义:
对数据库表色一列或多列的值进行排序的一种结构
(Btree 方式)
优点:
加快数据的检索速度
缺点:
1. 需要占用物理存储空间
2. 当对表中数据更新时, 索引需要动态维护. 降低
数据维护速度 占用系统资源
运行时间检测:
开启: set profiling=1;
关闭: set profiling=0;
查询 MySQL 变量: show variables like profiling;
查询执行记录: show profilings;
字段创建索引:
create index name on t1(字段名);
索引的分类:
1. 普通索引 (index)
使用规则:
1. 可设置多个字段
2. 字段值无约束
3.key 标志: MUL
创建 index
创建表时创建
create table(....
- ...
- index(字段名),
- index(字段名 2)...)
已有表添加 index
create index 索引名 on 表名 (字段名);
查看索引:
1.desc 表名; key:MUL
2.show index from 表名
3.show index from\G;
删除索引:
drop index 索引名 on 表名;
2. 唯一索引 (unique)
使用规则:
1. 可以设置多个字段
2. 约束: 字段值不允许重复, 但可以为 NULL
3.key 标志: UNI
创建 unique:
1. 创建表时创建
unique(字段名),..
2. 已有表
create unique index 索引名 on 表名 (字段名);
查看, 删除 和普通索引一致
3. 主键索引 (primary key)
自增属性 (auto_increment, 配合主键一起使用)
使用规则:
1. 只能有一个主键字段
2. 约束: 不允许重复, 且不能为 NULL
3.key 标志: PRI
4. 通常设置记录编号字段 id, 能唯一锁定一条记录
创建 primary key
创建表时:
1.id int primary key auto_increment,
2. 起始值: 表 ()auto_inctement=10000;
已有表:
alter table 表名 add primary key(id);
添加: alter table 表名 modify id int auto_inctement;
删除:
1. 删除自增属性 (modify)
alter table 表名 modify id int;
2. 删除主键索引
alter table 表名 drop primary key;
4. 外键索引........
算法全是 btree 节省时间都一样 不同的是约束不同
这里 btree 算法 有人说 btree 就是 btree 不是二叉树 但是我觉得就是二叉树 没什么区别
根据数据量的大小 提升速度 快能达到几百倍的提速
数据导入:
作用:
把文件系统的内容导入到数据库
语法:
load data infile "文件名"
into table 表名
- fields terminated by "分隔符"
- lines terminated by "\n";
步骤:
1. 数据库中创建对应的表
2. 把文件拷贝到数据库的默认搜索路径中
1. 查看默认路径
- show variables like "secure_file_priv";
- /var/lib/mysql-files/
2. 拷贝文件
sudo cp ~/scoretable.csv /var/lib/mysql-files/
3. 把表导入到数据库
数据导出:
作用:
数据库中表的记录导出到系统文件里
语法:
select ... from 表名
- into outfile "/var/lib/mysql-files / 文件名"
- fields terminated by "分隔符"
- lines terminated by "\n";
步骤:
1. 直接执行导出命令
2. 自动创建文件
3. 默认导出到默认搜索路径
文件权限:
rwxrw-rw- 1 tarena tarena
所有者 所属组
rwx:tarena 用户
rw-: 同组其他用户
rw-: 其他组的用户 (mysql)
- r: 4
- w: 2
- x: 1
最高权限: 7
查看权限: ls -l 文件名
修改文件权限: chmod 644 文件名
Excel 表格如何转化为 CSV 文件
打开 Excel 文件 -> 另存为 -> CSV(逗号分隔)
更改文件编码格式
用记事本 / 编辑器 打开, 文件 -> 另存为 -> 选择编码
导入示例:
将 scoretable.csv 文件导入到数据库的表中
1, 在数据库中创建对应的表
- create table scoretab(
- id int,
- name varchar(15),
- score float(5,2),
- number bigint,
- class char(7)
- );
2, 把文件拷贝到数据库的默认搜索路径中
1, 查看默认搜索路径
- show variables like "secure_file_priv";
- /var/lib/mysql-files/
2, 拷贝文件
sudo cp ~/scoretable.csv /var/lib/mysql-files/
3, 执行数据导入语句
- load data infile "/var/lib/mysql-files/scoretable.csv"
- into table scoretab
- fields terminated by ","
- lines terminated by "\n";
导出示例:
把 MOSHOU 库下的 sanguo 表英雄的姓名, 攻击值, 国家导出来, sanguo.txt
- select name,gongji,country from MOSHOU.sanguo
- into outfile "/var/lib/mysql-files/sanguo.txt"
- fields terminated by " "
- lines terminated by "\n";
将 mysql 库下的 user 表中 user,host 两个字段的值导出到 user.txt
- select user,host from mysql.user
- into outfile "/var/lib/mysql-files/user.txt" fields terminated by " "
- lines terminated by "\n";
查询
- $ sudo -i
- $ cd /var/lib/mysql-files/
- $ ls
- $ cat sanguo.txt
来源: https://www.cnblogs.com/ParisGabriel/p/9404696.html