-
'''
特性:
原子性:一组操作要么都成功,要么都失败
一致性:只事务发生前和发生后,数据的总额依然匹配
隔离性:某个事务的操作对其他事务不可见
持久性:当事务完成后,其影响应该保留下来,不能撤销
开启事务:start transcation
提交失败:commit
回滚:rollback
'''
start transaction
update 表名 set 列值 where id = 1;
commit | rollback
#ps:针对mysql,当 (drop table 表名) 是无法rollback的回去的 -
存储引擎
engine = innodb
'''
存储引擎主要分为两类:
Innodb:默认版本包含5.5
支持事务
不支持全文索引
索引和数据都是在一个文件中(.idb),表结构在.frm 文件中
MyIsam:默认版本5.5以下,不包含5.5
不支持事务
支持全文索引
.frm表结构,.myd表数据,.myi表索引
memory:基本不用
全文索引(中文):sphinx
''' -
索引
'''
主要用来加快查询速度,可以将索引理解成一个特殊的文件.如果没有这个文件,查询是从前到后查找数据的,如果有这个文件的话,会按照一种特殊的数据结构(二叉树)查找数据
主键索引:加快查询 + 不能重复 + 不能为空 primary key
唯一索引:加快查询 + 不能重复 unique(列名)
联合唯一索引: unique(列名)
普通索引:加快查询 index('列名')
'''
#在建表完成后想要加索引
create table t1(
id int auto_increment primary key
)
主键索引: alter table 表名 change 列名 新列名 列属性
唯一索引
方法一:create unique index 索引名称(ix_name) on (表名)(列名)
create unique index ix_dsize on department (dsize)
方法二:alter table 表名 add unique(列名)
alter table t1 add unique(列名)
普通索引: create index 索引名称(ix_name) on (表名)(列名)
create index ix_name on t1(c1)
#删除
drop index 索引名称(ix_name) on 表名
drop index ix_name on t1
alter table t1 drop index c1
#ps:使用了索引名称的方式去建立索引,那么删除就要使用删除索引名称的方式,建议使用索引名称去建立索引
#索引的缺点
版本5.3一下:删除和修改的速度就变慢了
版本5.5一上:删除和修改的速度不是特别的慢
#索引的使用:
explain工具,查看sql语句是否用的上索引,或者查看sql执行效率的工具,给执行的sql语句出一个报告,通过此报告来判断sql语句的执行效率和效果
explain select * from t1;
SQL语句的规则:
不建议使用 like 进行搜索 (elasticsearch:分布式多用户的全文搜索引擎)
组合索引最左前缀
如果组合索引为:(name,email)
where name and email -- 使用索引
where name -- 使用索引
where email -- 不使用索引 -
慢日志查询
'''
慢日志文件:记录了执行速度特别慢的sql语句
'''
show variables like '%query%'
set global long_query_time = 1
set global slow_query_log = ON
slow_query_log_file = D:xxxxxx
general log 普通日志
SQL审计 记录sql的操作语句
show variables like '%general%';
set global general_log = ON; -
权限管理
#创建
create user 用户名 @ IP地址 identified by 密码
create user 'zhq' @ '192.168.1.%' identified by 123
create user 'zhq' @ '%' identified by 123
#删除
drop user '用户名'@'IP地址'
#修改用户
rename user '用户名' @ 'ip地址' to '新用户名' @ 'ip地址'
#修改密码
set password for '用户名' @ 'ip地址' = Password('新密码')
#授权
grant 权限 on 数据库.表 to '用户' @ 'ip地址'
grant select on db1.* to 'root' @ '%'
grant select on *.* to 'root'@'%'
grant select,insert,delete on db1.* to 'root'@'%'
#刷新
flush privileges;
#ps:*和%都是表示所有,%表示所有的ip,*表示所有的数据库或者表.授权和创建操作必须执行flush之后才会生效