mysql增删改查
#### create
- alter 修改表结构 add/change/drop 列名/字段/属性 类型
change只能修改列的类型,不能修改列的名字
- 添加约束
> alter table scores add constranint stu_score foreign key(stuid) references students(id)
update table set column1=value1,column2=value2,... where
condition
修改数据,修改属性值
delete from table where condition
物理删除
select
原始数据集和结果集,结果集基于原有数据集合过滤提取。
- 查询全部
对列筛选
select * from table
select column1,cloumn2 from table
- 去重
去重某个字段中相同的值
select distinct gender from table
select distinct gender,id from table
- 条件
逐行筛选,逐行判断
where 逐行筛选,并把知足要求的行放到结果集中。
- 比较运算符
< > = !=
- 逻辑运算符
and or not
- 模糊查找
- like
- %表示多个任意字符
- _表示任意一个字符
select * from table where name like "%鹏%"
select * from table where name like "_鹏"
select * from student where name like "黄%" or name like "%靖"
- 范围查询
- in表示在非连续的范围
> select * from where id in(1,3,8)
- between ... and ... 表示一个连续的范围
> select * from where id between 3 and 8
- 空判断
- 注意 null与''是不一样的
> null不指向内存 ''内存存储空值
- 判空 is null
> select * from student where birthday is null
- 判断非空 is not null
> select * from student where birthday is not null
- 优先级
> 小括号,not,比较运算符,逻辑运算符
> and 优先于 or
### 聚合函数
> 将现有的多行数据统计,将原始数据集统计成结果集
> mysql提供了经常使用的5个聚合函数
> count() max(column) min(column) sum(column) avg(column)
> select count(*) from table where isDelete = 1
> 先拿到原始数据集过滤,而后聚合
### 分组
- 分组的目的仍是为了聚合,把此字段相同的数据组合在同一个组,更好的进行数据统计
-语法
> select column1,column2,column3 from table group by column1,column2,column3
select gender as 性别,count(*) from table group by gender;
### 分组后的数据筛选
- **语法**:
> select column1,column2,column3 from table group by column1,column2,column3 having column1 = xxx
>having功能至关于where,不一样点是having必须加上group by 上;where是对原始数据集筛选,having是对group by分组后的结果集筛选
### 排序
- 为了方便查看数据,能够对数据进行排序。
- 语法:
> select * from table order by column1 asc|desc, column2 asc|desc
> select * from students where isDelete = 0 and gender = 1 order by id desc;
> update subjects set isDelete=1 where title in('linux','redhat')
### 分页
- 当数据量过大时,在一页中查看数据是一件很是麻烦的事情
- 语法
> select * from table_name limit start,count
- 从start开始,获取count条数据
- start索引从0开始
### 示例:实现分页
- 已知:每页显示m条数据,当前显示第n页
- 求总页数:此段逻辑后面也会在python/golang中实现
- 查询总条数p1
- p1/m=p2
- 若是整除则p2为总页数
- 若是不整除则p2+1为总页数
- 求第n页的数据
> select * from students where isDelete = 0 limit (n-1)*m,m
### 总结
- 完整select语句
```
select distinct *
from table_A inner|left|right join table_B on table_A.xxx = table_B.xxx
where ...
group by ... having ...
order by ...
limit start,count
```
- 执行顺序为
```
from table_name
where
group by
select distinct *
having
order by
limit start,count
```
mysql高阶操做
### 简介
- 实体与实体之间有三种对应关系
- 视图用于完成查询语句的封装
- 事务能够保证复杂增删改查有效
- 当数据量巨大时,为了提升查询速度能够经过建立索引实现
### 关系
> 示例 crate 三张表 students score subjects
>成绩表须要引用学生表和科目表
> 关系分为1 to 1 1 to n n to n
> 学生表 1 to 成绩表 n;成绩表 1 to 学生表 1;
> 科目表 1 to 成绩 n;成绩表 1 to 科目表 1;
> so把关系创建在成绩表
### 创建表关系
> 关系也是一种字段,须要要表中建立
> 先设计E-R模型中表结构关系,再建立约束
### 外键约束
>保证数据有效性
>先肯定表与表之间是否有关系,再肯定时几对几的关系,而后为了确认数据有效性,创建外键约束
```
crate table scores (
id int primary auto_increment,
stuid int
subid int
score decimal(5,2),
foreign key(stuid) references students(id),
foreign key(subid) references subjects(id)
);
```
### 外键的级联操做
- 级联操做的类型
- restrict(限制):默认值,抛异常
- cascade(级联):若是主表的记录删除,则从表中相关联的记录会被删除
- set null:将外键设置为空
- no action:什么都不作
### 链接查询
>使用场景:须要用到的信息来源于多张表,找到表与表之间的关系再链接查询
- table_A inner join table_B: table_A 与table_B匹配的行会出如今结果中
- table_A left join table_B:table_A 与table_B匹配的行会出如今结果中,外加外表table_A独有的数据,未对应的数据使用null填充
- table_A right join table_B:table_A 与table_B匹配的行会出如今结果中,外加外表table_B独有的数据,未对应的数据使用null填充
> 三种链接主要的区别是结果集的区别:
```
select students.name,subjects.tile,scores.score
from scores
inner join students on scores.stuid = students.id
inner join subjects on scores.subid = students.id;
```
rename oldname to newname
engine不同 底层的数据结构不同
备份和恢复
备份
mysqldump -uroot -p database_name > backup_db.sql
恢复(注意要先建立数据库)
mysqldump -uroot -p database_name < backup_db.sql
### 产品设计
确认有哪些实体,确认实体是几对几的关系,确认在哪一个实体中创建哪些字段
#### 示例
- 购物车
- 消费者
- 商品
- 数量
- 价格
- 商品信息
- 名称
- 价格
- 单位
- 日期
...
> 分析对应关系 1个购物车对应1个商品信息
#### 练习
> 查询男生的姓名、总分
> 使用sum,考虑使用分组,按人名分组
```
select students.name,sum(scores.score) from students inner join scores on scores.stuid = students.id
where students.gender = 1
group by students.id
```
### 自关联
> 同一张表中不一样字段关联,好比 pid refrences aid 把省,市,县放在同一个表中,减小表开销。那pid 关联 aid就称为自关联
### 视图
- 视图本质是对查询语句的封装
- 对于复杂的查询,在屡次使用后,维护是一件很是麻烦的事情,解决:定义视图
```
create view stuscore as
select students.*,scores.score
from studejts inner join scores on students.id = scores.stuid
where
//查询视图
select * from stuscore
```
```
create view view_name as statements
```
### 事务
- 一个业务逻辑须要执行多条sql语句,若是某个sql语句出错,则但愿真个操做都回退,保证业务逻辑的完整性
- 事务的四大特性(ACID)
- 原子性(Atomicity) 要么所有成功,要么所有失败
- 一致性(Consistency) 几个并行执行的事务,其执行结果必须与按某一顺序串行执行的结果一致。
- 隔离性(Isolation) 事务执行不受其余事务干扰,事务执行的中间结果必须对其余事务是透明的
- 持久性(Durability):对已经提交的事务,其数据必须被持久存储
- 要求:表的类型必须是 innodb或者bdb类型,才可使用事务
- innodb 行级锁
>查看建表语句
```
show crate table students;
```
- 事务语句
>使用事务的前提条件:当存在insert/update/delete
```
begin;
update students set name = "michael" where id = 1 //内存级的临时表,而且上行级锁
//持久化存储数据并解锁
commit
//抛出异常后回滚
rollback
```
### 索引
- 为了优化查询,提升数据库的访问速度须要创建索引
- 主键和惟一索引都是索引,能够提升速度
#### 选择列的数据类型
- 数据小,简单数据类型,避免null
#### 操做
- 索引分单列索引和组合索引
- 单列索引 一个索引只包含单个列
- 组合索引,一个索引包含多个列
- 查看索引
```
show index from table_name;
```
- 建立索引及删除索引
```
create index index_name on table(username(length))
DROP index [index_name] on table
```
- 缺点:更新表时速度会变慢
- 启用运行监测
set profiling = 1
- 查看运行时间
show profiles