一、mysql存储引擎
1.1 MySQL工作原理
1.2 MySQL 存储引擎
• 作为可插拔式的组件提供
– MySQL 服务软件自带的功能程序,处理表的处理器
– 不同的存储引擎有不同的功能和数据存储方式
• 默认的存储引擎
– MySQL 5.0/5.1 ---> MyISAM
– MySQL 5.5/5.6 ---> InnoDB
1.2.1 查看数据库服务支持的存储引擎
mysql> show engines;
InnoDB DEFAULT
1.2.2 查看已有的表使用的存储引擎
show create table 表名;
1.2.3 修改数据库服务默认使用的存储引擎
]#vim /etc/my.cnf
[mysqld]
default-storage-engine=myisam
...
]# systemctl restart mysqld
1.2.4 修改表/建表指定表的存储引擎
alter table 表 engine=存储引擎名;
create table 表(字段列表)engine=存储引擎名;
1.2.5 常用存储引擎的特点
innodb存储引擎特点:
支持事务 、 事务回滚、外键
支持行级锁 (客户端连接数据库服务器后,对表的数据做访问时,若表的存储引擎是innodb的话,会只给表中被访问的行加锁)
存储方式: 一个表对应2个存储文件
表名.frm 表结构
表名.ibd 数据和索引
myisam存储引擎特点:
不支持事务 、 事务回滚、外键
支持表级锁 (客户端连接数据库服务器后,对表的数据做访问时,若表的存储引擎是myisam的话,会给整张表加锁)
存储方式: 一个表对应3个存储文件
表名.frm 表结构
表名.MYD 数据
表名.MYI 索引
1. 事务(Transactions):一次sql操作从建立连接到操作完成断开连接的访问过程给称作事务。
-- 支持事务的可以做事务回滚 :一次sql操作有任意一步没有执行成功会恢复所有操作。(对innodb存储引擎的表 访问时 必须任意一步操作都成功,才能完成操作。)
2. mysql数据库服务使用事务日志文件记录对innodb存储引擎表执行的sql操作。
cd /var/lib/mysql/
ib_logfile0 -|
|------> 记录SQL命令
ib_logfile1 -|
insert into t1 values(8888);
ibdata1 ----> 数据源(sql命令执行后产生的数据信息)
1.2.6 MySQL 锁机制
锁粒度:
表级锁(myisam)给整张表加锁
行级锁 (innodb) 只给表中当前被操作行加锁
锁的作用:解决对表的并发访问冲突问题。 (读锁和写锁是由sql语句决定的,行锁和表锁是由存储引擎决定的)
select * from t1 where id <=20;
insert
delete from t1;
update t1 set name="bob" where name="lucy";
update t1 set name="tom" where name="jerry";
锁类型:
读锁(共享锁) 当对一张表执行查询(select)操作时 会加读锁
写锁(排他锁或互斥锁) 当对一张表执行写(insert update delete)操作时 会加写锁
1.2.7 存储引擎的选择
执行写操作多的表适合使用innodb存储引擎,可以并发访问。
update t1 set name="bob" where id=3;
update t1 set name="bob" where id=4;
执行查操作多的表适合使用myisam存储引擎,可以节省系统资源。
select * from t1 where id <=10;
select * from t1 where id >10;
二、数据导入导出(批量操作数据)
2.1 数据导入
基本用法
-- LOAD DATA INFILE "目录/文件名"
INTO TABLE 库.表
FIELDS TERMINATED BY "列间隔符号"
LINES TERMINATED BY "\n"注意事项
-- 字段分隔符要与文件内的一致
--指定导入文件的绝对路径
--导入数据的表字段类型要与文件字段匹配
--禁用Selinux保护机制
1.查看数据库的默认导入导出文件存放目录
mysql> show variables like "secure_file_priv";
2.修改默认导入导出文件目录
[root@host50 ~]# mkdir /mydata
[root@host50 ~]# chown mysql /mydata
[root@host50 ~]# vim /etc/my.cnf
[mysqld]
.. secure_file_priv="/mydata" ..
[root@host50 mydata]# systemctl restart mysqld //重启服务
mysql> show variables like "secure_file_priv";
3.创建存储数据表(表结构参考/etc/passwd)
create database db3;
create table db3.usertab(
username char(50),
password char(1),
uid int(2),
gid int(2),
comment char(100),
homedir char(100),
shell char(50),
index(username)
);
desc db3.usertab;
4.把需要导入的文件放到默认导入导出目录下
cp /etc/passwd /mydata
5.用load命令导入数据
load data infile "/mysqldata/passwd"
into table db3.usertab
fields terminated by ":"
lines terminated by "\n";
6.为usertab表添加id字段为主键并且自动增长
mysql> alter table db3.usertab add id int(2) primary key auto_increment first;
mysql> select * from db3.usertab;
mysql> select * from db3.usertab where id=20;
2.2 数据导出
基本用法
--SQL查询 into outfile "目录名/文件名"
filds terminated by "列分隔符"
lines terminated by "\n";注意事项
--导出的内容由SQL查询语句决定
--导出的是表中的记录,不包括字段名
-- 禁用SELinux
1.把查询到的数据导出到文件中
mysql>select username,uid from db3.usertab into outfile "/mysqldata/user1.txt";
mysql>select * from db3.usertab into outfile "/mysqldata/user2.txt";
mysql>select username,uid from db3.usertab into outfile "/mysqldata/user3.txt" fields terminated by "###";
# cat /mysqldata/user1.txt
# cat /mysqldata/user2.txt
# cat /mysqldata/user3.txt
三、管理表记录
3.1 插入记录
语法格式:
• 格式 1 :给所有字段赋值
– INSERT INTO 表名
VALUES
( 字段 1 值, .. .. ,字段 N 值 ) ,
( 字段 1 值, .. .. ,字段 N 值 ) ,
( 字段 1 值, .. .. ,字段 N 值 ) ,
.. .. ;• 格式 2 ,给指定字段赋值
– INSERT INTO 表名 ( 字段 1,.. .., 字段 N)
VALUES
( 字段 1 值,字段 2 值,字段 N 值 ) ,
( 字段 1 值,字段 2 值,字段 N 值 ) ,
( 字段 1 值,字段 2 值,字段 N 值 ) ,
.. .. ;
插入记录 insert into (值要与字段类型和约束条件匹配)
插入N条记录给所有字段赋值
insert into 库.表 values(字段值列表),(字段值列表);
mysql> insert into usertab values (50,"yaya2","x",1002,1002,"","/home/yaya2","/sbin/nologin"),(51,"7yaya","x",1003,1003,"","/home/7yaya","/sbin/nologin");
插入N条记录给指定的字段赋值
insert into 库.表(字段名列表) values(字段值列表),(字段值列表);
insert into usertab(username,homedir,shell) values
("lu8cy","/home/lu8cy","/bin/bash"),("tom","/home/tom","/bin/bash"),("lilei","/home/lilei","/bin/bash");
3.2 查看记录
语法格式:
• 格式 1
– SELECT 字段 1, .. .., 字段 N FROM 表名 ;
• 格式 2
– SELECT 字段 1, .. .., 字段 N FROM 表名 WHERE 条件表达式 ;
• 注意事项
– 使用 * 可匹配所有字段
– 指定表名时,可采用 库名 . 表名 的形式
查看表中所有行的所有字段的值
select * from 库.表 ;
select * from db3.usertab;
查看表中所有行的指定字段的值
select 字段名1,字段名2 ,字段名n from 库.表 ;
select id,username,password from db3.usertab;
查看指定行的指定字段的值
select 字段名1,字段名2 ,字段名n from 库.表 where 匹配条件 ;
select username,uid,shell from usertab where id = 1;
3.3 修改记录
语法格式
• 格式 1 ,更新表内的所有记录
– UPDATE 表名
SET
字段 1= 字段 1 值 ,
字段 2= 字段 2 值 ,
字段 N= 字段 N 值 ;• 格式 2 ,只更新符合条件的部分记录
– UPDATE 表名
SET
字段 1= 字段 1 值 ,
字段 2= 字段 2 值 ,
字段 N= 字段 N 值 ;
WHERE 条件表达式 ;
修改所有记录指定字段的值
update 库.表 set 字段名=值,字段名=值;
update db3.usertab set password="A" ;
修改与条件匹配的记录指定字段的值
update 库.表 set 字段名=值,字段名=值 where 匹配条件 ;
update db3.usertab set password="x" where id=1;
3.4 删除记录
语法格式:
• 格式 1 ,仅删除符合条件的记录
– DELETE FROM 表名 WHERE 条件表达式 ;
• 格式 2, 删除所有的表记录
– DELETE FROM 表名 ;
删除表中的所有行
delete from 库.表;
delete from db3.usertab
仅删除与条件匹配的记录
delete from 库.表 where 匹配条件 ;
delete from db3.usertab where id=3;
四、匹配条件
4.1 基本查询条件
4.1.1 数值比较 (格式:字段名 符号 数字)
- 字段类型必须是数据数值类型
类型 | 用途 |
= | 等于 |
> 、 >= | 大于、大于或等于 |
<、<= | 小于、小于或等于 |
!= | 不等于 |
select username from usertab where uid=10;
select id,username,uid from usertab where uid=1001;
select * from usertab where id<=10;
4.1.2 字符比较/匹配空/非空 (格式: 字段名 符号 "字符串")
- 字符比较时,字段类型必须是字符类型
类型 | 用途 |
= | 等于 |
!= | 不等于 |
IS NULL | 匹配空 |
IS NOT NULL | 非空 |
select username from usertab where username="apache";
select username,shell from usertab where shell!="/bin/bash";
select username,uid,gid from usertab where uid is null and gid is null;
select id from usertab where name="yaya" and uid is not null;
4.1.3 范围内匹配/去重显示
- 匹配范围内的任意一个值即可
类型 | 用途 |
in(值列表) | 在...里... |
not in(值列表) | 不在...里... |
between 数字1 and 数字2 | 在...之间... |
distinct 字段名 | 去重显示 |
查询不显示字段重复值 distinct 字段名(distinct只能用在查询中)
select username from usertab where uid between 100 and 150;
select username,uid from usertab where uid in (10,20,30,50);
select username from usertab where username not in ("root","bin");
select distinct shell from usertab where uid >10 and uid<=100;
4.1.4 逻辑匹配(就是有个查询条件)
- 多个判断条件时使用
类型 | 用途 |
OR | 逻辑或 |
AND | 逻辑与 |
! | 逻辑非 |
() | 提高优先级 |
select username,uid from usertab where username="root" and uid=0 and shell="/bin/bash";
select username,uid from usertab where username="root" or uid=1 or shell="/bin/bash";
select username,uid from usertab where username="root" or username="apache" or username="bob";
4.2 高级查询条件
4.2.1 模糊匹配
• 基本用法
– WHERE 字段名 LIKE ' 通配字串 '
– 通配符 _ 匹配单个字符
– % 匹配 0~N 个字符
select username from usertab where username like '_ _ _ _';
select username from usertab where username like 'a_ _t';
select username from usertab where username like 'a%';
select username from usertab where username like '_%_';
select id,name from user where name like '%'; //匹配0个到多个
4.2.2 正则匹配(匹配有共同条件的数据,模糊匹配)
• 基本用法
– WHERE 字段名 REGEXP ' 正则表达式‘
– ^ $ . * [ ] (范围内) | (或)
select username from usertab where username regexp '[0-9]';
select username from usertab where username regexp '^[0-9]';
select username from usertab where username regexp '[0-9]$';
select username from usertab where username regexp 'a.*t'; //包含a和t,且at之间有零个或多个字符
select username from usertab where username regexp '^a.*t$'; //以a开头,且以t结尾,at之间有零个或多个字符
select username,uid from usertab where uid regexp '..'; //有两个字符就可以,模糊匹配
select username,uid from usertab where uid regexp '^..$'; //严格匹配只包含两个字符
4.2.3 四则运算(select 和 update 操作是可以做数学计算)
字段类型必须数值类型(整型 或浮点型)
类型 | 用途 |
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取余数 |
select id,username,uid from usertab where id <=10;
update usertab set uid=uid+1 where id <=10;
select username ,uid,gid, uid+gid as zh , (uid+gid)/2 as pjz from usertab where username="mysql";
select username , age , 2018-age s_year from usertab where username="root";
4.2.4 聚集函数(对字段的值做统计,字段的类型要求是数值类型;不能够做条件判断)
类型 | 用途 |
count(字段名) | 统计字段值的个数 |
sum(字段名) | 求和 |
max(字段名) | 输出字段值的最大值 |
min(字段名) | 输出字段值的最小值 |
avg(字段名) | 输出字段值的平均值 |
select max(uid) from usertab;
select sum(uid) from usertab;
select min(uid) from usertab;
select avg(uid) from usertab;
select count(id) from usertab;
select count(username) from usertab where shell="/bin/bash";
mysql> select count(*) from user; //所有字段只要一个不为空就会就会算进去
mysql> select count(id),count(name) from user;
mysql> select name,id from user where name is null;
错误用法
select name,max(gid) from user; //max(gid)的值只有一个,name有很多个。
select name from user where gid=max(gid); //聚合函数不能作为查询条件
4.3 操作查询结果
4.3.1 查询分组 (group by:查询什么字段,就用什么去分组)
• 基本用法
– SQL 查询
group by 字段名 (字段名通常是字符类型)
sql查询 group by 字段名;
select shell from usertab where uid >10 and uid<=100 group by shell;
4.3.2 查询排序 (按照数值类型的字段排队)
• 基本用法
– SQL 查询
ORDER BY 字段名 [ asc | desc ] (字段名通常是数值类型)
select username,uid from usertab where uid >10 and uid<=100 order by uid;
select username,uid from usertab where uid >10 and uid<=100 order by uid desc;
4.3.3 限制查询显示行数(默认显示所有查询的记录)
• 基本用法
– SQL 查询 LIMIT N; 显示查询结果前 N 条记录
– SQL 查询 LIMIT N,M ;显示指定范围内的查询记录
– SQL 查询 where 条件查询 LIMIT N ;显示查询结果前 N 条记录
– SQL 查询 where 条件查询 LIMIT N , M ;显示指定范围内的查询记录
select username,uid from usertab where uid >10 and uid<=100 order by uid desc limit 1;
select username,uid from usertab where uid >10 and uid<=100 order by uid desc limit 2,3;
4.12 having在查询结果里面过滤数据
查询结果过滤
• 基本用法
– SQL 查询 HAVING 条件表达式;
– SQL 查询 where 条件 HAVING 条件表达式;
– SQL 查询 group by 字段名 HAVING 条件表达式;
mysql> select name,uid from user where uid>=1000 having name is not null;
mysql> select name,uid from user where uid>=1000 having uid=200;
mysql> select name,uid from user where uid>=1000 having name="bob";