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