MySQL 索引 詳解

2021-10-14 04:06:03 字數 2933 閱讀 1951

刪除索引

檢視索引

選擇索引的原則

索引作為一種資料結構,其用途是用於提公升檢索資料的效率

1,普通索引(index)

在建立表時指定索引

mysql> create table student1(id int not null, name varchar(100) not null, birthdy date, *** char(1) not null, index nameindex (name(50)));

query ok, 0 rows affected (0.02 sec)

基於表結構建立

mysql> create table student2(id int not null, name varchar(100) not null, birthday date, *** char(1) not null);

query ok, 0 rows affected (0.01 sec)

mysql> create index nameindex on student2(name(50));

修改表結構建立

mysql> create table student3(id int not null, name varchar(100) not null, birthday date, *** char(1) not null);

query ok, 0 rows affected (0.01 sec)

mysql> show index from student3; \\檢視索引

2,唯一索引(unique)

在建立表時指定索引

mysql> create table student4(id int not null, name varchar(100) not null, birthday date, *** char(1) not null, unique index id_idex (id));

query ok, 0 rows affected (0.00 sec)

基於表結構建立

mysql> create table student5(id int not null, name varchar(100) not null, birthday date, *** char(1) not null);

query ok, 0 rows affected (0.00 sec)

mysql> create unique index idindex on student5(id);

3,主鍵索引(primary key)

建立表時指定索引

mysql> create table student6(id int not null, name varchar(100) not null, birthday date, *** char(1) not null, primary key (id));

query ok, 0 rows affected (0.01 sec)

修改表結構建立索引

mysql> create table student7(id int not null, name varchar(100) not null, birthday date, *** char(1) not null);

query ok, 0 rows affected (0.01 sec)

mysql> alter table student7 add primary key (id);

query ok, 0 rows affected (0.01 sec)

records: 0 duplicates: 0 warnings: 0

1,刪除普通索引

直接刪除索引

mysql> drop index nameindex on student1;
修改表結構刪除索引

mysql> alter table student2 drop index nameindex;
2,刪除唯一 索引(unique)

直接刪除索引

mysql> drop index idindex on student4;
修改表結構刪除索引

mysql> alter table student drop index idindex;
3,刪除主鍵索引(primary key)

主鍵索引不能採用直接刪除索引的方式刪除

修改表結構刪除索引

mysql> alter table student drop primary key;
mysql> show index from 表名;
index(key)每張表可以有很多列做index,必須的起名

面試題

導致sql執行慢的原因:

MySQL聚集索引詳解 mysql 索引詳解

直接起飛 1.什麼是索引?索引是幫助mysql高效獲取資料的排好序的資料結構。2.索引的資料結構?為什麼選這種結構?假設我們現在這裡有一張表 以下情況都是innodb儲存引擎 idnumber 如果mysql沒有索引這種結構,那麼我們如果查詢number為51的這行記錄,那麼mysql就要從上往下掃...

MySQL聚集索引詳解 mysql索引詳解

資料結構分,有b tree索引 b tree 雜湊索引 r tree索引等。按資料塊的順序和索引節點的邏輯順序是否一致可以分為聚集索引和非聚集索引。聚集索引由於物理塊連續,在範圍掃瞄的時候可以減少磁頭尋道時間,因而比非聚集索引高效。幾種索引型別的選擇 primary 主鍵索引。unique 唯一索引...

mysql 索引定義 MySQL 索引詳解

普通索引 唯一索引和主索引 1.普通索引 普通索引 由關鍵字key或index定義的索引 的唯一任務是加快對資料的訪問速度。因此,應該只為那些最經常出現在查詢條件 where column 或排序條件 order by column 中的資料列建立索引。只要有可能,就應該選擇乙個資料最整齊 最緊湊的...