CREATE INDEX

语法说明

在表中创建索引,以便更加快速高效地查询数据。

你无法看到索引,索引只能被用来加速搜索/查询。

更新一个包含索引的表需要比更新一个没有索引的表花费更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。

索引有两种常见的类型,分别为:

  • Primary Key:即主键索引,即标识在主键列上的索引。
  • Secondary Index:即二级索引,即在非主键上标识的索引。

语法结构

  1. > CREATE [UNIQUE] INDEX index_name
  2. ON tbl_name (key_part,...)
  3. COMMENT 'string'

语法释义

CREATE UNIQUE INDEX 语法

在表上创建一个唯一的索引。不允许使用重复的值:唯一的索引意味着两个行不能拥有相同的索引值。

示例

  1. drop table if exists t1;
  2. create table t1(id int PRIMARY KEY,name VARCHAR(255),age int);
  3. insert into t1 values(1,"Abby", 24);
  4. insert into t1 values(2,"Bob", 25);
  5. insert into t1 values(3,"Carol", 23);
  6. insert into t1 values(4,"Dora", 29);
  7. create unique index idx on t1(name);
  8. mysql> select * from t1;
  9. +------+-------+------+
  10. | id | name | age |
  11. +------+-------+------+
  12. | 1 | Abby | 24 |
  13. | 2 | Bob | 25 |
  14. | 3 | Carol | 23 |
  15. | 4 | Dora | 29 |
  16. +------+-------+------+
  17. 4 rows in set (0.00 sec)
  18. mysql> show create table t1;
  19. +-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | Table | Create Table |
  21. +-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | t1 | CREATE TABLE `t1` (
  23. `id` INT NOT NULL,
  24. `name` VARCHAR(255) DEFAULT NULL,
  25. `age` INT DEFAULT NULL,
  26. PRIMARY KEY (`id`),
  27. UNIQUE KEY `idx` (`name`)
  28. ) |
  29. +-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  30. 1 row in set (0.01 sec)
  31. create table t2 (
  32. col1 bigint primary key,
  33. col2 varchar(25),
  34. col3 float,
  35. col4 varchar(50)
  36. );
  37. create unique index idx on t2(col2) comment 'create varchar index';
  38. insert into t2 values(1,"Abby", 24,'zbcvdf');
  39. insert into t2 values(2,"Bob", 25,'zbcvdf');
  40. insert into t2 values(3,"Carol", 23,'zbcvdf');
  41. insert into t2 values(4,"Dora", 29,'zbcvdf');
  42. mysql> select * from t2;
  43. +------+-------+------+--------+
  44. | col1 | col2 | col3 | col4 |
  45. +------+-------+------+--------+
  46. | 1 | Abby | 24 | zbcvdf |
  47. | 2 | Bob | 25 | zbcvdf |
  48. | 3 | Carol | 23 | zbcvdf |
  49. | 4 | Dora | 29 | zbcvdf |
  50. +------+-------+------+--------+
  51. 4 rows in set (0.00 sec)
  52. mysql> show create table t2;
  53. +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | Table | Create Table |
  55. +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | t2 | CREATE TABLE `t2` (
  57. `col1` BIGINT NOT NULL,
  58. `col2` VARCHAR(25) DEFAULT NULL,
  59. `col3` FLOAT DEFAULT NULL,
  60. `col4` VARCHAR(50) DEFAULT NULL,
  61. PRIMARY KEY (`col1`),
  62. UNIQUE KEY `idx` (`col2`) COMMENT `create varchar index`
  63. ) |
  64. +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. 1 row in set (0.01 sec)