文章目录
命名
主键 PK_stu
唯一 UQ_stu
默认 DF_stu
检查 CK_stu
外键 FK_stu
用户操作
-- 创建用户
create user 'angel'@'localhost' identified by '123';
-- 让普通用户可以添加用户
grant create user on *.* to angel@'localhost' identified by '123';
-- 回收用户权限
revoke create user on *.* from 'angel'@'localhost';
-- 更改密码
set password for 'root'@'localhost'=password('123');
-- 删除用户
drop user egon@'localhost';
SQL 的四个组成部分
- 数据定义语言,即SQL DDL,用于定义SQL模式、基本表、视图、索引等结构。
- 如: create、drop、alter 等
- 数据操纵语言,即SQL DML。数据操纵分成数据查询和数据更新两类。
- 如: insert、update、delete
- 数据查询语言,即SQL DQL。
- 如: select
- 数据控制语言,即SQL DCL,这一部分包括对基本表和视图的授权、完整性规则的描述、事务控制等内容。
- grant、revoke、commit、rollback
结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统, 可以使用相同的结构化查询语言作为数据输入与管理的接口。结构化查询语言语句可以嵌套,这使它具有极大的灵活性和强大的功能。
库操作
-- 建库
create database db01;
-- 删库
drop database db01;
-- 创建一个使用utf8字符集的库
create database db01 character set utf8;
-- 创建一个使用utf8字符集, 并带有较对规则的库
create database db01 character set utf8 collate utf8_bin;
-- 较对规则: utf8_bin 区分大小写 默认utf8_general_ci不区分大小写
# 查询语句
select * from t1 where name = 'tom'; -- 查询name为tom的记录
# 显示数据库语句
show databases;
-- 显示数据库创建语句
show create database db01;
-- 在创建数据库时, 为了规避关键字, 可以使用反引号解决
create database `create`;
drop database `create`;
-- 备份数据库
mysqldump -u 用户名 -p -B 数据库1 数据库2 数据库n >文件名.sql
mysqldump -u root -p -B db01 > /Users/angel/bak.sql;
-- 恢复数据库
source 路径文件名.sql
-- 备份表
mysqldump -u 用户名 -p 数据库 表1 表2 > /Users/angel/bak.sql;
表创建
-- 创建表
create table table_name(
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校对规则 engine 引擎;
-- field: 指定列名 datatype: 指定列类型(字段类型)
-- character set: 如不指定则为所在数据库字符集
-- collate: 如不指定则为所在数据库校对规则
-- engine: 存储引擎
show tables; -- 查看当前库所有表包括视图
表结构修改 alter
-- 添加字段
alter table 表名
add column 字段名 [default expr] [, column 字段名]...;
-- 修改字段
alter table 表名
modify column 字段名 [default expr] [, column 字段名]...;
-- 删除字段
alter table 表名
drop column 字段名;
-- 查看表结构
desc 表名;
-- 修改表名
rename table 表名 to 新表名
-- 修改字符集
alter table 表名
character set 字符集
-- 修改字段名
alter table 表名
change column 旧字段名 新字段名 类型 参数;
-- 应用实例
alter table emp
add column image varchar(32) after id; -- 添加字段 添加到id后面
alter table emp
modify column job varchar(60); -- 修改字段
alter table emp
drop column sex; -- 删除字段
rename table emps to employee; -- 更改表名
alter table employee character set utf8;-- 修改字符集
alter table employee
change name user_name varchar(20);-- 修改字段名和类型
-- 添加主键
alter table student
add constraint PK_studentno primary key(studentno);
-- 添加外键
alter table student
add foreign key (外键) references 主表(主键);
数据库 C[create] R[read] U[update] D[delete]语句
插入记录 insert
-- 插入语句
insert into tablename[(column)[,column]]
values (value [, value]);
# 说明insert语句的细节
-- 1. 插入的数据应与字段的数据类型相同
-- 2. 数据的长度应在字段的规定范围内
-- 3. 在values中列出的数据位置必须与被加入的字段的排列位置相对应
-- 4. 字符和日期型数据应包含在单引号中
-- 5. 字段可以插入空值[前提是该字段允许为空], insert into table value(null)
-- 6. insert into tab_name(字段名) values (),(),() 形式添加多条记录
-- 7. 如果是给表中的所有字段添加数据, 可以不写前面的字段名称
-- 8. 默认值的使用, 当不给某个字段值时, 如果有默认值就会添加, 否则会报错
修改记录update
-- 演示update语句
-- 要求: 在上面创建的employee表中修改表中的记录
-- 1. 将所有员工的薪水修改为5000元 如果没有带where条件会修改所有的记录
update employee
set salary = 5000;
-- 2. 将姓名为 小妖怪 的员工薪水修改为3000元
update employee
set salary = 3000
where user_name = '小妖怪';
-- 3. 将老妖怪的薪水在原有基础上增加1000元
update employee
set salary = salary + 1000
where user_name = '老妖怪';
-- 修改多个字段
update employee
set salary = salary + 2000, job = '出主意'
where user_name = '老妖怪';
删除记录 delete
-- 使用delete语句删除表中数据
delete from tb1_name
[where where_definition];
-- 删除表中名称为'老妖怪'的记录
delete from employee
where user_name = '老妖怪';
-- 删除表中所有的记录
delete from employee;
-- 使用细节
-- 1. 如果不使用where语句, 将删除表中所有的数据
-- 2. delete语句不能删除某一列的值(可使用 update 设为 null 或者 '')
-- 3. 使用delete语句仅删除记录, 不删除表本身, 如果要删除表, 使用drop table语句
查询select
基本查询
-- 基本语法
select [distinct | distinctrow] * | [column1, column2...]
from tablename;
-- 过滤表中重复数据 distinct
select distinct english from student;
-- 要查询的记录, 每个字段都相同, 才会去重
-- 使用别名表示学生分数
select name as '名字', (chinese + english + math + 10) as '总分'
from student;
-- 使用 order by 子句排序查询结果
select column1, column2, column3...
from table
order by column1 asc | desc, ... -- 默认asc升序
-- order by 指定排序的列, 排序的列既可以是表中的字段, 也可以是select语句后指定的列名
-- asc 升序[默认]、desc 降序
-- order by 子句应位于 select 语句的结尾
-- 对数学升级排序后输出
select * from student
order by math;
-- 对总分按从高到低的顺序输出
select name, (chinese + english + math) as total_score from student
order by total_score desc;
-- 对姓韩的学生成绩排序输出
select name,(chinese + english + math) as total_score from student
where name like '韩%'
order by total_score;
where 运算符
-- 在where语句中经常使用的运算符
-- 比较运算符
-- > < <= >= = <> != 大于、小于、小于(大于)等于、不等于
-- 字段 between ... and ... 显示在某一区间的值 包含头尾
-- in(set) 显示在in列表中的值, 例: in(100, 200)
-- 字段 like '张%' 模糊查询 %: 表示0到多个任意字符 _: 表示单个任意字符
-- not like 模糊查询
-- is null 判断是否为空
-- 逻辑运算符
-- and 多个条件同时成立
-- or 多个条件任一成立
-- not 不成立
-- 练习
-- 查询总分大于 200 分的所有同学
select name, (chinese + english + math) as '总分' from student
where (chinese + english + math) > 200;
-- 查询总分大于200分并且数学成绩小于语文成绩的姓赵的学生
select * from student
where (chinese + english + math) > 200 and
math < chinese and name like '赵%'; -- 模糊匹配
-- 查询英语分数在 80 - 90 之间的同学
select * from student
-- where english >= 80 and english <= 90;
where english between 80 and 90; -- between .. and .. 是闭区间
-- 查询数学分数为89, 90, 91的同学
select * from student
-- where math = 89 or math = 90 or math = 91;
where math in (89, 90, 91);
-- 查询所有姓韩的学生及成绩
select * from student
where name like '韩%';
查询加强
-- 显示1992-01-01后入职的员工
select * from emp where hiredate > '1992-01-01';
-- 显示第三个字符为大些O的所有员工的姓名和工资
select ename, sal from emp where ename like '__O';
--
select * from emp where mgr is null;
--
select * from emp order by deptno asc, sal desc;
分组查询
-- group by
-- group by 用于对查询的结果分组统计
-- having
-- having 语句用于限制分组显示结果
-- 注意:
-- select 列表中只能包含: 1. 被分组的列 2. 为每个分组返回一个值的表达式, 如聚合函数
# group by + having
-- having 子句用于限制分组显示结果
-- 如何显示每个部门的平均工资和最高工资
select avg(sal), max(sal), deptno
from emp group by deptno;
-- 显示每个部分的每种岗位的平均工资和最低工资
select avg(sal), min(sal), deptno, job
from emp group by deptno, job;
-- 显示平均工资低于2000的部门号和它的平均工资
select avg(sal), deptno
from emp group by deptno
having avg(sal) < 2000;
分页查询
-- select ... limit start, rows
-- 表示从start+1行开始取, 取出rows行, start从0开始计算
select * from emp limit 0, 3;
select * from emp limit 3, 3;
select * from emp limit 6, 3;
select * from emp limit 每页显示记录数 * (第几页 - 1), 每页显示记录数;
3 0 3
3 1 3
3 2 3
使用顺序
group by > having > order by > limit
多表查询
select * from emp, dept;
-- 在默认情况下: 当两个表查询时
-- 1. 从第一张表中取出一行和第二张表的每一行进行组合 返回结果[含有两张表的所有列]
-- 2. 一共返回的记录数 第一张表的记录数 * 第二张表的记录数
-- 3. 这样多表查询默认处理返回的结果, 称为笛卡尔集
-- 4. 解决这个多表的关键就是要写出正确的过滤条件
---------------------------------------------------------------
-- 自连接 是指在同一张表的连接查询[将同一张表看做两张表]
select worker.ename as "职员名", boss.ename as "上级名"
from emp as worker, emp as boss
where worker.mgr = boss.empno;
-- 自连接的特点:
-- 1. 把同一张表当作两张表使用
-- 2. 需要给表取别名 表名 表别名
-- 3. 列名不明确, 可以指定列的别名
---------------------------------------------------------------
-- 内连接 inner join
select ename, dname from emp
inner join dept
on dept.deptno = emp.deptno;
-- 外连接
-- 左外连接 left join
select ename, dname from emp -- 以这个表为基准
left join dept
on dept.deptno = emp.deptno;
-- 右外连接 right join
select ename, dname from emp
right join dept -- 以这个表为基准
on dept.deptno = emp.deptno;
子查询
-- 返回与 30部门员工工资水平相同的员工姓名与工资。
select * from emp
where sal in (
select sal from emp
group by sal
having count(sal) > 1
) and deptno = '30';
>ALL() 表示大于所有(大于最大值)
>SOME() 表示至少大于一个(大于最小值)
exists()
约束语句
- not null 非空约束
- unique 唯一约束
- primary key 主键约束
- foreign key(外键字段) references 主表(主键字段) 外键约束
- default 默认值约束
- auto_increment 递增约束
create table student(
id int primary key auto_increment,
name varchar(10) not null,
class varchar(8),
phone char(11) unique
);
create table score(
id int,
chinese double default '0.0',
math double default '0.0',
english double default '0.0',
foreign key(id) references student(id)
);
alter table student auto_increment=6; --
视图
-- 创建视图(虚拟表)
-- create view view_name as ...;(查询结果)
create view view_name as
select ename, hiredate, mname, mgrdate from emp
inner join mgrs on mgrs.mgr = emp.mgr
where mgrdate > hiredate;
-- 查询视图
select * from view_name;
-- 删除视图
drop view view_name;
-- 修改视图
alter view view_name as ...;
索引
-- 添加索引
create [unique][fulltext][normal]
index index_name
on table_name(column_name...)
-- 删除索引
drop index index_name on table_name;
存储过程
create procedure pro_stu(in p_sname varchar(20))
begin
select * from studentinfo where studentname=p_sname;
end;
set @p_sname='李四';
call pro_stu(@p_sname);
函数
聚合函数
-- 统计函数 count
-- 返回行的总数
-- select count(*) | count(列名) from tablename
-- [where ...]
count(*) 和 count(列) 的区别
-- 解释: count(*) 返回满足条件的记录的行数
-- count(列): 统计满足条件的某列有多少个, 但是会排除为null的情况
create table t15(
name varchar(20)
);
insert into t15 values('tom'),('jack'), ('mary'), (null);
select count(*) from t15; -- 4
select count(name) from t15; -- 3
-- 合计函数 sum
-- sum函数返回满足where条件的行的和
-- select sum(列名) [, sum(列名)...] from tablename
-- [where ...]
-- 注意: sum 仅对数值起作用, 否则会报错
-- 平均函数 avg
-- avg 函数返回满足where 条件的一列的平均值
-- select avg(列名) {,avg(列名)...} from tablename
-- [where ...]
-- 最大最小值函数 max/min
-- max/min 函数返回满足where 条件的一列的最大/最小值
-- select max(列名) from tablename
-- [where ...]
字符串函数
-- charset(str) 返回字符集
select charset(ename) from tablename;
-- concat (string2 [, ...]) 连接字符串 将多个列拼接成一列
select concat(ename, ' job is ', job) from emp;
-- instr(string, substring) 返回substring在string中的位置, 没有返回0
select instr('韩顺平', '平') from dual; -- dual 亚元表 系统表 可以作为测试表使用
-- ucase(string) 转换成大写
select ucase(ename) from emp;
-- lcase(string) 转换成小写
select lcase(ename) from emp;
-- left (string, length) 从string中的左边起取length个字符
select left(ename, 2) from emp;
select right(ename, 2) from emp; # 右边
-- length(string) string 长度[按照字节]
select length(ename) from emp;
-- replace(str, search_str, replace_str) 在str中用replace_str替换search_str
select ename, replace(job, 'manager', '经理') from emp;
-- strcmp(string, string2) 逐字符比较两字串大小
select strcmp('hsp', 'hsp') from dual;
-- substring(str, position [, length]) 从str的position开始[从1开始计算], 取length个字符
-- 从ename列的第一个位置开始取出两个字符
select substring(ename, 1, 2) from emp;
-- ltrim(string) 去除左边的空格 rtrim(string) 去除右边的空格 trim(string)去除前后端的空格
select ltrim(' 雨天烦 ') from dual;
select rtrim(' 雨天烦 ') from dual;
select trim(' 雨天烦 ') from dual;
-- 练习: 以首字母小写的方式显示所有员工emp表的姓名
-- 方式一
select concat(lcase(substring(ename, 1, 1)), substring(ename, 2)) from emp;
-- 方式二
select concat(lcase(left(ename, 1)), substring(ename, 2)) from emp;
数学函数
-- abs(num) 绝对值
select abs(-10) from dual;
-- bin(decimal_number) 十进制转二进制
select bin(10) from dual;
-- ceiling(number) 向上取整, 得到比num2 大的最小整数
select ceiling(1.1) from dual;
-- conv(number, from_base, to_base) 进制转换
select conv(255, 10, 2) from dual; -- 把88当作10进制转成2进制输出
-- floor(number) 向下取整 得到比number小的最大整数
select floor(-1.1) from dual;
-- format(number, decimal_places) 保留小数位数(四舍五入)
select format(78.123458, 2) from dual;
-- hex(decimalNumber) 转十六进制
select hex(12) from dual;
-- least(number, number2 [, ...]) 求最小值
select least(0, 1, -10, 4) from dual;
-- mod(numerator, denominator) 求余
select mod(10, 3) from dual;
-- rand([seed]) 返回随机数 其范围为 0 < v < 1.0
-- 1. 如果使用rand()每次返回不同的随机数 范围0 < v < 1.0
-- 2. 如果使用rend(seed) 返回随机数, 范围0 < v < 1.0, 如果seed不变, 该随机数也不变了
select rand() from dual;
时间日期相关函数
-- current_date()当前日期
select current_date() from dual;
-- current_time()当前时分秒
select current_time() from dual;
-- current_timestamp()当前年月日时分秒
select current_timestamp() from dual;
-- 返回datetime的日期部分 2021-09-07 07:22:47 -> 2021-09-07
select date(`time`) from dual;
-- date_add 在date中加上日期或时间
-- date_add(time, interval int type) 请查询在10分钟内发布的帖子
select * from mes
where date_add(send_time, interval 10 minute) >= now();
-- date_sub()在date上减去一个时间
select * from mes
where date_sub(now(), interval 10 minute) <= send_time;
-- datediff(date1, date2) 两个日期差(结果是天)
select
datediff('2011-11-11', '1990-01-01') / 365
from dual;
-- timediff(date1, date2) 两个日期差(结果是时分秒)
select timediff('10:11:11', '06:10:10') from dual;
-- now() 当前时间
select now();
-- year | month | day | date(datetime)
select year(now()) from dual;
-- from_unixtime 返回的是1970-1-1到现在的秒数
select unix_timestamp() from dual;
-- unix_timestamp();
加密和系统函数
-- user() 查询用户
-- 可以查看登陆到mysql的有哪些用户, 以及登陆的IP
select user() from dual; -- 用户@地址
-- database() 查询当前使用的数据库名称
select database() from dual;
-- md5(str) 为字符串算出一个md5 32的字符串, (密码加密)
-- root 密码 angel -> 加密 -> 在数据库中存放的是加密后的密码
select md5('angel') from dual;
select length(md5('angel')) from dual;
-- password(str) 加密函数 mysql 8.0已移除
select password('angel') from dual;
-- select * from mysql.user \G
流程控制函数
-- if(expr1, expr2, expr3) 如果expr1为true, 则返回expr2 否则返回expr3
select if(true, '北京', '上海') from dual;
-- ifnull(expr1, expr2) 如果expr1不为null, 则返回expr1, 否则返回expr2
select ifnull(null, 'angel') from dual; -- angel
select ifnull('jack', 'angel') from dual; -- jack
-- select case
-- when expr1 then expr2
-- when expr3 then expr4
-- ...
-- else expr5
-- end;
-- [类似多重分支]
-- 如果expr1 为true, 则返回expr2, 如果expr2为true, 则返回expr4, 否则返回expr5
select case
when false then 'jack'
when false then 'angel'
else '231'
end as 'result' from dual;
-- 判断是否为null 使用 is null 判断不为null使用 is not
事务
-
什么是事务
-
事务用于保证数据的一致性, 它由一组相关的dml语句组成, 该组的dml语句要么全部成功, 要么全部失败
-
-
事务和锁
-
当执行事务操作时(dml语句), mysql会在表上加锁, 防止其他用户改表的数据 这对用户来讲是非常重要的
-
mysql 数据库控制台事务的几个重要操作
- start transaction 开始一个事务
- savepoint 保存点名 设置保存点
- rollback to 保存点名 回退事务
- rollback 回退全部事务
- commit 提交事务, 所有的操作生效, 不能回退
- 细节
- 没有设置保存点
- 多个保存点
- 存储引擎
- 开始事务方式
- 细节
-
-
回退事务
在介绍回退事务前, 先介绍一下保存点(savepoint). 保存点事事务中的点. 用于取消部分事务, 当结束事务时, 会自动的删除该事务所定义的所有保存点
当执行回退事务时, 通过指定保存点可以回退到指定的点
-
提交事务
使用 commit 语句可以提交事务. 当执行了commit语句后, 会确认事务的变化、结束事务、删除保存点、释放锁、数据生效. 当使用commit语句结束事务后其它会话 [其他连接] 将可以查看到事务变化后的新数据 [所有数据就正式生效]
-
事务细节
- 如果不开始事务, 默认情况下, dml操作时自动提交的, 不能回滚
- 如果开始一个事务, 你没有创建保存点, 你可以执行 rollback, 默认就是回退到你事务开始的状态
- 你也可以在这个事务中(还没有提交时), 创建多个保存点. 比如: savepoint a; 执行 dml, savepoint b;
- 你可以在事务没有提交前, 选择回退到哪个保存点
- mysql事务机制需要 innodb的存储引擎才可以使用, myisam不好使
- 开始一个事务 start transaction;, set autocommit=off; 两种方式开启事务
-
事务隔离级别
-
多个连接开启各自事务操作数据库中数据时, 数据库系统要负责隔离操作, 以保证各个连接在获取数据时的准确性(通俗解释)
-
如果不考虑隔离性, 可能会引发如下问题:
- 脏读 (dirty read)
- 当一个事务读取另一个事务尚未提交的修改(update, insert, delete)时, 产生脏读
- 不可重复读 (nonrepeatable read)
- 同一查询在同一事务中多次进行, 由于其它提交事务所做的修改或删除, 每次返回不同的结果集, 此时发生不可重复读
- 幻读 (phantom read)
- 同一查询在同一事务中多次进行, 由于其它提交事务所做的插入操作, 每次返回不同的结果集, 此时发生幻读
- 脏读 (dirty read)
-
mysql 隔离级别定义了事务与事务之间的隔离程度
Mysql隔离级别 脏读 不可重复读 幻读 加锁读 读未提交 (Read uncommitted) √ √ √ 不加锁 读已提交 (Read committed) X √ √ 不加锁 可重复读 (Repeatable read) X X X 不加锁 可串行化 (Serializable) [演示重开客户端] X X X 加锁 √ 是指可能出现, X 是指不会出现
select @@tx_isolation; -- 低版本mysql 查看当前隔离级别 select @@transaction_isolation; -- 高版本mysql查看当前隔离级别 select @@global.transaction_isolation; -- 查看系统当前隔离级别 set autocommit = 0 || 1 -- 值为0关闭自动提交 值为1开启自动提交 set session transaction isolation level serializable || read uncommitted || read committed || repeatable read; -- 设置隔离级别 -- mysql配置文件设置隔离级别 [mysqld] transaction-isolation=repeatable-read || ...
-
-
mysql 事务ACID特性
-
原子性 (Atomicity)
原子性是指事务是一个不可分割的工作单位, 事务中的操作要么都发生, 要么都不发生
-
一致性 (Consistency)
事务必须使数据库从一个一致性状态 (提交了之后) 变换到另一个一致性状态
-
隔离性 (Isolation)
事务的隔离性是多个用户并发访问数据库时, 数据库为每一个用户开启的事务, 不能被其他事务的操作数据所干扰, 多个并发事务之间要相互隔离
-
持久性 (Durability)
持久性是指一个事务一旦被提交, 它对数据库中数据的改变就是永久性的, 接下来即使数据库发生故障也不应该对其有任何影响
-
存储引擎
-
基本介绍
- MySql 的表类型有存储引擎 (Storage Engines) 决定, 主要包括MyISAM、innoDB、Memory 等
- MySql 等数据表主要支持六种类型, 分别是: CSV、Memory、ARCHIVE、MRG_MYISAM、MYISAM、InnoDB
- 这六种又分为两类, 一类是’事务安全型’ (transaction-safe) 比如: InnoDB; 其余都属于第二类, 称为’非事务安全型’ (non-transaction-safe) [mysiam 和 memory]
-
主要的存储引擎/表类型特点
特点 | Myisam | InnoDB | Memory | Archive |
---|---|---|---|---|
批量插入的速度 | 高 | 低 | 高 | 非常高 |
事务安全 | 支持 | |||
全文索引 | 支持 | |||
锁机制 | 表锁 | 行锁 | 表锁 | 行锁 |
存储限制 | 没有 | 64TB | 有 | 没有 |
B树索引 | 支持 | 支持 | 支持 | |
哈希索引 | 支持 | 支持 | ||
集群索引 | 支持 | |||
数据缓存 | 支持 | 支持 | ||
索引缓存 | 支持 | 支持 | 支持 | |
数据可压缩 | 支持 | 支持 | ||
空间使用 | 低 | 高 | N/A | 非常低 |
内存使用 | 低 | 高 | 中等 | 低 |
支持外键 | 支持 |
-
细节说明 重点说三种: MyISAM、InnoDB、MEMORY
- MyISAM 不支持事务、也不支持外键, 但其访问速度快, 对事务完整性没有要求
- InnoDB 存储引擎提供了具有提交、回滚和崩溃恢复能力的事务安全, 但是比起 MyISAM 存储引擎, InnoDB 写的处理效率差一些并且会占用更多的磁盘空间以保留数据和索引
- MEMORY 存储引擎使用存在内存中的内容来创建表, 每个MEMORY表只实际对应一个磁盘文件. MEMORY类型的表访问非常快, 因为它的数据时放在内存中的, 并且默认使用HASH索引. 但是一旦MySql服务关闭, 表中的数据就会丢失掉, 表的结构还在
-
如何选择表的存储引擎
- 如果你的应用不需要事务, 处理的只是基本的 CRUD 操作, 那么 MyISAM 是不二选择, 速度快
- 如果需要支持事务, 选择InnoDB
- Memory 存储引擎就是将数据存储在内存中, 由于没有磁盘 I/O 的等待, 速度极快, 但由于是内存存储引擎, 所做的任何修改在服务器重启后都将消失.[经典用法 用户的在线状态]
show engines; -- 查看所有存储引擎
create table t1 (
id int,
name varchar(32)
) engine myisam; -- 添加速度快 不支持外键和事务 支持表级锁
-- memory 1. 数据存储在内存中 2. 执行速度很快(没有IO读写) 3. 默认支持索引(hash表)
-- 修改存储引擎
alter table 表名 engine = InnoDB;