MySQL 数据库知识点总结
MySQL 是一种关系型数据库管理系统,目前版本为 5.7。在学习 MySQL 之前,需要了解数据库的发展阶段:人工阶段、文件系统阶段和数据库阶段。常见的主流数据库有 MySQL、Oracle、SQL Server 等。
数据库模型有层次模型、网状模型和关系模型等。关系模型是目前最常用的模型。
### 库的操作
* 查看所有数据库:Show databases;
* 创建数据库:Create database 库名;
* 删除数据库:Drop database 库名;
### 表的操作
* 创建表:Create table 表名(列名 数据类型,….列名 数据类型);
* 查看所有表:Show tables;
* 查看表结构:Desc 表名;
* 修改表名:Alter table 旧表名 rename 新表名;
* 修改数据类型:Alter table 表名 modify 列名 新数据类型;
* 添加列:Alter table 表名 add 列名 数据类型;
* 删除列:Alter table 表名 drop 列名;
* 修改列名:Alter table 表名 change 旧列名 新列名 新数据类型;
* 删除表:Drop table 表名;
### 数据的操作
* 插入数据:Insert [into] 表名 (列名 1,列名 2….) values(值 1,值 2….);
* 插入数据:Insert [into] 表名 values(值 1,值 2….);
* 修改表数据:Update 表名 set 列名=值;
* 修改表数据:Update 表名 set 列名=值 where 条件;
* 删除表数据:Delete from 表名;
* 删除表数据:Truncate table 表名;
### 数据查询
* 查询数据:Select 列名 from 表名 where 条件;
* 去重查询:Select distinct(列名) from 表名;
* 限制条数查询:Select 列名 from 表名 limit 条数;
* 处理 null 值:Select 列名 from 表名 where 列名 is null/is not null;
* 排序查询:Select 列名 from 表名 order by 列名 asc/desc;
* 分组查询:Select 分组的列名 from 表名 group by 列名;
* Having 条件筛选:Having 跟 where 的区别,where 后面跟普通过滤条件,having 后面跟函数过滤条件
### 数据完整性
* 自增主键:auto_increment primary key
* 非空约束:not null
* 唯一约束:unique
* 默认约束:default ‘默认值’
### 运算符
* 比较运算符:看课本
* 逻辑运算符:And 与、Or 或、Not 非
### 聚合函数
* Sum() 求和
* Max() 最大值
* Min() 最小值
* Avg() 平均值
### 常用的函数
* 用于连接字符串:concat()
* 用于获取字符串长度:length()
* 截取指定长度字符串:substring()
* 替换字符串的值:replace()
* 四舍五入:round()
### 事务的隔离级别
* Read uncommitted 读未提交
* Read committed 读已提交
* Repeatable read 重复读
* Serializable 串行化
### 实例操作
* 创建用户表:Create table user(Id int auto_increment primary key,Name char(10) not null,Card varchar(18) unique,Gender char(3) default ‘男’);
* 查询员工信息:Select count(*) as 人数 from dept;
* 查询员工信息并按工号降序排列:Select * from dept order by id desc;
* 更新工号为 101 的员工,名字改为露露:Update dept set name=’露露’ where id=101;