O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Add index

You can add an index by executing the ALTER TABLE ADD INDEX command. For example, if you want to add an index on last_name, see the following code:

mysql> ALTER TABLE employees ADD INDEX (last_name);Query OK, 0 rows affected (1.28 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> SHOW CREATE TABLE employees\G*************************** 1. row ***************************       Table: employeesCreate Table: CREATE TABLE `employees` (  `emp_no` int(11) NOT NULL,  `birth_date` date NOT NULL,  `first_name` varchar(14) NOT NULL,  `last_name` varchar(16) NOT NULL,  `gender` enum('M','F') NOT NULL,  `hire_date` date NOT NULL,  PRIMARY KEY (`emp_no`),  KEY `name` (`first_name`,`last_name`),  KEY `last_name` (`last_name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required