CREATE INDEX

Description

Create indexes on tables to query data more quickly and efficiently.

You can’t see the index; the index can only be used to speed up the search/query.

Updating a table with an index takes longer than updating a table without an index because the index also needs to be updated. Therefore, the ideal approach is to create indexes only on frequently searched columns (and tables).

There are two common types of indexes, namely:

  • Primary Key: The primary key index, that is, the index identified on the primary key column.
  • Secondary Index: the secondary index, that is, the index identified on the non-primary key.

Syntax

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

Explanations

CREATE UNIQUE INDEX

Creates a unique index on a table. Duplicate values are not allowed.

Examples

  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)