Mysql索引、事務、視圖 經常使用命令及要點概括

索引:

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

做用:快速查詢數據 條件:數據條目大於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 回滾ide

補充:

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;

相關文章
相關標籤/搜索