一、索引
索引,是資料庫中專門用於幫助使用者快速查詢資料的一種資料結構。類似於字典中的目錄,查詢字典內容時可以根據目錄查詢到資料的存放位置,然後直接獲取即可。
分類:普通索引
唯一索引
全文索引
組合索引
主鍵索引
1、普通索引
普通索引僅有乙個功能:加速查詢
#建立表時建立索引
create table user (
id int,
username varchar(30) ,
password varchar(50),
index index_username (username)
注意:建立索引時如果是blob 和 text 型別,必須指定length。
2、唯一索引
唯一索引有兩個功能:加速查詢 和 唯一約束(可含null)
#建立表時建立唯一索引
create table user (
id int,
username varchar(30) ,
password varchar(50),
unique index_username (username)
);#在存在的表上建立唯一索引
create unique index 索引名 on 表名(列名)#刪除存在表上的索引
drop unique index 索引名 on 表名
3、全文索引
全文索引(fulltext)僅可以適用於myisam引擎的資料表;作用於char、varchar、text資料型別的列。
#建立表時建立唯一索引
create table user (
id int,
username varchar(30) ,
password varchar(50),
fulltext index_username (username)
4、組合索引
將幾個列作為一條索引進行檢索,使用最左匹配原則,比如頻繁的使用多列進行查詢,where username='bright' and password='123456'
#建立組合索引
create table user (
id int,
username varchar(30) ,
password varchar(50),
index index_info (username, password)
5、主鍵索引
主鍵有兩個功能:加速查詢 和 唯一約束(不可含null),當一張表把某個列設為主鍵的時候,則該列就是主鍵索引。
#建立主鍵索引
create table user (
nid intnot null auto_increment primary key, #主鍵索引
username varchar(30) ,
password varchar(50),
index index_info (username, password)
#建立主鍵alter table 表名 add primary key(列名);
#刪除主鍵
alter table 表名 drop primary key;
6、索引的新增與刪除
(1)新增索引
#在已存在的表上建立索引
create [unique | fulltext |spatial ] index 索引名
on 表名 (欄位名[(長度)] [asc|desc]) ;#如:
creeate index index_name on table_name(column_name)
(2)刪除索引
#刪除存在表上的索引
drop index 索引名 on 表名#如:
drop index_name on table_name;
(3)檢視索引
#檢視存在表上的索引
show index from 表名#如:
show index from table_name;
二、事務
1、概念
事務是乙個最小的不可再分的工作單元或者一組原子性的sql命令,一旦在執行命令過程**現錯誤就會回滾到最初狀態,從而保證資料的完整性。事務必須滿足四個條件:
原子性:乙個事務(transaction)中的所有操作,要麼全部完成,要麼全部不完成,不會結束在中間某個環節。
一致性:在事務開始之前和事務結束以後,資料庫的完整性沒有被破壞。
隔離性:資料庫允許多個併發事務同時對其資料進行讀寫和修改的能力,隔離性可以防止多個事務併發執行時由於交叉執行而導致資料的不一致。
永續性:事務處理結束後,對資料的修改就是永久的,即便系統故障也不會丟失
2、事務相關命令
--開啟事務:start transaction--事務結束:end transaction--提交事務:commit transaction--回滾事務:rollback transaction
3、例項
事務操作成功
mysql> start transaction; --開始事務
query ok, 0 rows affected (0.00sec)
mysql> insert into class(caption) values('四年一班');
query ok,1 row affected (0.00sec)
mysql> commit; --提交,資料庫內部已經改變
query ok, 0 rows affected (0.00sec)
mysql> select * fromclass;+-----+----------+
| cid | caption |
| 1 | 三年二班 |
| 2 | 一年三班 |
| 3 | 三年一班 |
| 5 | 四年一班 |
4 rows in set (0.00 sec)
事務操作失敗
mysql> start transaction; --開啟事務
query ok, 0 rows affected (0.00sec)
mysql> insert into class(caption) values('四年一班');
query ok,1 row affected (0.00sec)
mysql> select * fromclass;+-----+----------+
| cid | caption |
| 1 | 三年二班 |
| 2 | 一年三班 |
| 3 | 三年一班 |
| 4 | 四年一班 |
4 rows in set (0.00sec)
mysql> rollback; --回滾,事務操作失敗
query ok, 0 rows affected (0.00sec)
mysql> select * fromclass;+-----+----------+
| cid | caption |
| 1 | 三年二班 |
| 2 | 一年三班 |
| 3 | 三年一班 |
3 rows in set (0.00 sec)
三、檢視
檢視可以理解為儲存的查詢語句,當呼叫的時候,產生結果集,檢視充當的是虛擬表的角色(不是真實存在的)。
1、建立檢視
#格式:create view 檢視名稱 assql語句create view v as
select * from userinfo;
2、刪除檢視
#格式:drop view檢視名稱drop view v
3、修改檢視
格式:alter view 檢視名稱 assql語句alter view v asselet username,passwordfrom userinfo;
4、使用檢視
由於檢視是虛擬表,因此無法使用其對真實表進行建立、更新和刪除操作,僅能做查詢用。
select * from v;
四、儲存過程
儲存過程是乙個sql語句集合,當主動去呼叫儲存過程時,其中內部的sql語句會按照邏輯執行。
1、建立儲存過程
mysql> delimiter $$ --將語句的結束符號從分號;臨時改為兩個$$(可以是自定義)
mysql> create procedurep2()-> select * fromusertabl-> end$$
query ok,0 rows affected (0.0)
mysql> delimiter ; --#將語句的結束符號恢復為分號
2、執行儲存過程
mysql> call p2();
3、刪除儲存過程
mysql> drop procedure p2;
mysql高階操作 MySQL資料庫的高階操作
1.資料備份與還原 1 備份 mysqldump mysqldump u username p password dbname tbname1 tbname2.filename.sql mysqldump u root p mydb2 c mysql dump mydb2 dump.sql 2 恢復...
mysql 高階 資料庫鎖
是什麼?鎖是計算機協調多個程序或執行緒併發訪問某一資源的機制。在資料庫中,除傳統的計算機資源的徵用以外,資料也是一種供許多使用者共享的資源。如何保證資料併發訪問的一致性 有效性是所有資料庫必須解決的乙個問題,鎖衝突也是影響資料庫併發訪問效能的乙個重要因素。從這個角度來說,鎖對資料庫極為重要。鎖的分類...
MySQL資料庫高階查詢
mysql查詢 子查詢 exists和not exists子查詢 exists子查詢 在執行create 或drop語句前,可以使用exists判斷該資料庫物件是否存在,返回值是true或false。drop table if exists temp exists也可以作為where語句的子查詢 s...