共計 3437 個字符,預計需要花費 9 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章主要介紹 mysql 數據庫中怎么創建索引,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
案例:創建數據庫 index_test,按照下表的結構在 index_test 數據庫中創建兩個數據表 test_table1 和 test_table2,并按照操作過程完成對數據表的基本操作。
(1)登錄 MySQL 數據庫
(2) 創建數據庫 index_test
(3)創建表 test_table1
(4)創建表 test_table2,存儲引擎為 MyISAM
(5)使用 alter table 語句在表 test_table2 的 birth 字段上建立名稱為 ComDateIdx 的普通索引
(6) 使用 alter table 語句在表 test_table2 的 id 字段上添加名稱為 UniqIdx2 的唯一索引,并以降序排列
(7) 使用 create index 在 firstname、middlename 和 lastname 三個字段上建立名稱為 MultiColidx2 的組合索引
(8) 使用 create index 在 title 字段上建立名稱為 FTidx 的全文索引
(9) 使用 alter table 語句刪除表 test_table1 中名稱為 Uniqidx 的唯一索引
(10) 使用 drop index 語句刪除表 test_table2 中名稱為 MultiColidx2 的組合索引
幾個注意點
(1)登錄 MySQL 數據庫
C:\Users\Hudie mysql -h localhost -u root -p
Enter password: *******
(2)創建數據庫 index_test
mysql create database index_test;Query OK, 1 row affected (0.06 sec)mysql use index_test;Database changed
(3)創建表 test_table1
mysql create table test_table1 - (
- id int not null primary key auto_increment,
- name char(100) not null,
- address char(100) not null,
- description char(100) not null,
- unique index uniqidx(id),
- index MultiColidx(name(20),address(30) ),
- index Comidx(description(30))
- );Query OK, 0 rows affected (0.11 sec)mysql show create table test_table1 \G*************************** 1. row ***************************
Table: test_table1Create Table: CREATE TABLE `test_table1` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(100) NOT NULL,
`address` char(100) NOT NULL,
`description` char(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniqidx` (`id`),
KEY `MultiColidx` (`name`(20),`address`(30)),
KEY `Comidx` (`description`(30))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)
可以看到在 test_table 表中成功創建了 3 個索引,分別是在 id 字段上名稱為 uniqidx 的唯一索引;在 name 和 address 字段上的組合索引;在 description 字段上長度為 30 的普通索引。
(4)創建表 test_table2,存儲引擎為 MyISAM
mysql create table test_table2 - (
- id int not null primary key auto_increment,
- firstname char(100) not null,
- middlename char(100) not null,
- lastname char(100) not null,
- birth date not null,
- title char(100) null
- )ENGINE=MyISAM;Query OK, 0 rows affected (0.07 sec)
(5)使用 alter table 語句在表 test_table2 的 birth 字段上建立名稱為 ComDateIdx 的普通索引
mysql alter table test_table2 add index ComDateidx(birth);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0
(6)使用 alter table 語句在表 test_table2 的 id 字段上添加名稱為 Uniqidx2 的唯一索引
mysql alter table test_table2 add unique index Uniqidx(id);Query OK, 0 rows affected (0.11 sec)Records: 0 Duplicates: 0 Warnings: 0
(7)使用 create index 在 firstname 和 middlename 兩個字段上建立名稱為 MultiColidx2 的組合索引
mysql create index MultiColidx2 on test_table2(firstname,middlename);Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0
(8)使用 create index 在 title 字段上建立名稱為 FTidx 的全文索引
mysql create fulltext index ftidx on test_table2(title);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0
(9)使用 alter table 語句刪除表 test_table1 中名稱為 Uniqidx 的唯一索引
mysql alter table test_table1 drop index uniqidx;Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0
(10)使用 drop index 語句刪除表 test_table2 中名稱為 MultiColidx2 的組合索引
mysql drop index MultiColidx2 on test_table2;Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0
幾個注意點:
1. 索引對數據庫的性能如此重要,如何使用它?
如果索引列較少,則需要的磁盤空間和維護開銷都較少。
如果在一個大表上創建了多種組合索引,索引文件也會膨脹很快。另外索引較多,可覆蓋更多的查詢。
嘗試添加、刪除、修改索引,不影響數據庫架構或應用程序設計。
2. 盡量使用短索引
對字符串類型的字段進行索引,如果可能應該指定一個前綴長度。例如,有一個 char(255)的列,如果在前 10 或 30 個字符內多數值是唯一的,就不需要對整個列進行索引。
短索引不僅可以提高查詢速度,也能節省磁盤空間、減少 I / O 操作。
以上是“mysql 數據庫中怎么創建索引”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注丸趣 TV 行業資訊頻道!
向 AI 問一下細節