1. 程式人生 > >Mysql索引、事務、視圖 常用命令及要點歸納

Mysql索引、事務、視圖 常用命令及要點歸納

commit lba ext 技術 歸納 工資 unique 定位技術 一致性

索引:

一種快速定位技術,相當於一本書的目錄頁.

作用:快速查詢數據 條件:數據條目大於2000條

create index id_index on info (id); //創建普通索引

show index from info \G //查看索引

drop index id_index from on info; //刪除索引

create unique index unique_id_index on info (id); //創建唯一索引

alter table info add primary key (id); //創建主鍵索引(已經創建了表,沒有指定主鍵,然後修改表加入主鍵,主鍵索引會自動創建)

alter table info change id id int(10);//刪除自增長

alter table info drop primary key;//刪除主建

alter table info add column age int; //添加列

alter table info drop column age int; //刪除列

create table infos (descript TEXT,FULLTEXT(descript)); //創建全文索引

create index multi_index on info(name,address); //創建多列索引

事務:

一組操作共同執行或者都不執行,結果保持一致。

特性:原子性、一致性、隔離性、持久性。

例如:銀行轉賬

姓名 余額 條件:余額>0
Zhangsan 100 /
lisi 200 /

註:Zhangsan 轉賬100給 to lisi

事務固定格式:

begin 開始
Update bank set money=money+100 where name=’lisi’
Update bank set money=money-100 where name=’zhangsan’
commit 提交
rollback 回滾

補充:

set autocommit = 1; //開啟自動提交

set autocommit = 0; //禁止自動提交

savepoint s2; //定義回滾點

rollback to savepoint s2; //回滾到S2 (相當於虛擬機還原快照)

視圖:

視圖是 數據庫中的虛擬表。

作用:一張表中的數據給不同的權限用戶提供訪問

舉例:公司員工績效工資考核表:

工號 姓名 年齡 崗位 績效 工資
1 Tom 50 總裁 / 100萬
2 Jerry 40 總監 90 20萬
3 charry 30 雲計算工程師 80 12萬
4 Jack 24 雲計算工程師 90 15萬

語法: create view 視圖名稱 AS select 語句

create view scoreview as select from info where score > 80; //創建視圖(條件:成績>80)

select * from score_view; //查看視圖

update score_view set score=88 where id=1; //id1的成績更新為88

drop view if exists score_view; //刪除視圖

總結:

1.數據庫索引分為普通索引、唯一性索引、主鍵索引、全文索引、多列索引;

2.數據庫索引可以協助快速查詢表中數據,但並不是任何字段都需要創建索引;

3.數據庫事務的ACID特性:原子性、一致性、隔離性、持久性;

4.MySQL事務命令有begin、rollback、commit、savepoint;

Mysql索引、事務、視圖 常用命令及要點歸納