English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Can there be function-based indexes in MySQL?

In below5.6In MySQL versions below, you cannot use function-based indexes. First, to create a function-based index in MySQL, we will create a table.

mysql> create table FunctionIndexDemo
   - > (
   - > FirstName varchar(100)
   - > );

Let's see the syntax for creating an index based on a function.

create index index_name on yourTableName (columnName(IntegerSize));

This is the query.

mysql> create index indFirstName on FunctionIndexDemo (FirstName(6));
Records: 0 Duplicates: 0 Warnings: 0

Check if the index exists.

mysql> SHOW INDEX FROM FunctionIndexDemo;

This is the output.

+-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| functionindexdemo |          1 | indFirstName |            1 | FirstName | A | | 0 |        6 | NULL | YES | BTREE | | | YES |
+-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
1 row in set (0.24 sec)