Is it possible to have a function-based index in MySQL?

MySQLMySQLi Database

Function-based index wasn’t possible in MySQL versions below 5.6. Firstly, to create function-based index in MySQL, we will create a table.

mysql> create table FunctionIndexDemo
   - > (
   - > FirstName varchar(100)
   - > );
Query OK, 0 rows affected (0.70 sec)

Let us see the syntax to create a function based index.

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

Here is the query.

mysql> create index indFirstName on FunctionIndexDemo (FirstName(6));
Query OK, 0 rows affected (0.56 sec)
Records: 0  Duplicates: 0  Warnings: 0

To check if the index is present.

mysql> SHOW INDEX FROM FunctionIndexDemo;

Here 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)
raja
Published on 21-Nov-2018 06:38:54
Advertisements