MySQL梳理知识点
MySQL:数据库管理系统(DBMS)
库:存储数据的地方
表:定义一种关系,用于数据与关系的对应
视图:可以看做一个虚拟表,表结构和数据由定义的查询语句决定
作用:提高代码重用性,安全,对外接口统一
劣势:效率低下,在连表的基础上不能继续增删改
触发器:增删改操作的条件触发
作用:自动执行某些操作,减少流程
劣势:定义时不校验触发语句的有效性
insert:new
delete:old
update:new、old
存储过程:相对于void方法,但是可以有出参,注重的是过程
函数:相对于有返回值的方法,注重的是返回值
索引:
主键primary key:不重复、查询检索速度快
唯一索引unique:基本同主键,但是可以有多个
普通索引Index:提高查询效率
全文索引:提高查询效率,只能在MyISAM引擎的表中使用
语句的分类:
DDL:对库和表本身的操作:create、drop、alter
DML:对数据的增删改:insert、update、delete
DQL:对数据的查询:select
DCL:赋权和事务支持:grant、commit、rollback
库的操作:
create database [if not exists] 库名;//创建库
drop database [if exists] 库名; //删除库
show databases; //查看库
grant select on 库名.表名 to '用户名'@'IP地址' identified by '密码'; //赋权
use 库名;
表的操作:
create table [if not exists] 表名(
字段名 类型(长度) [属性 索引][注释],
....
);
drop table [if exists] 表名;
alter table 表名 rename to 新表名;
alter table 表名 add column 字段名 类型(长度) [属性 索引][注释];
alter table 表名 modify 字段名 列类型[属性]
alter table 表名 change 旧字段名 新字段名 列类型[属性]
alter table 表名 drop 字段名
show tables;
desc 表名;
ahow create table 表名;
数据操作:
update 表名 set 列名=值 [,...] where 条件;
delete from 表名 where 条件;
truncate [table] 表名;
insert into 表名([字段列表]) values(对应的值列表)[,...];
select [distinct] {*|字段列表} from 表名
[left join|right join|inner join 表名 on 连接条件]
[where 条件]
[group by 分组字段]
[having 分组条件]
[order by 排序字段]
[limit a,b]
事务:
备份和恢复:
备份:
mysqldump [-h主机名] -u用户名 -p [options] 库名 [表名列表] > 文件路径;
恢复:
mysql [-h主机名] -u用户名 -p 库名 < 文件路径;
在mysql命令行:source 文件路径;
改变结束符
delimiter //
取消自动提交:
set autocommit=0;
恢复自动提交:
set autocommit=1;