在低于5.6的MySQL版本中,无法使用基于函数的索引。首先,要在MySQL中创建基于函数的索引,我们将创建一个表。
mysql> create table FunctionIndexDemo - > ( - > FirstName varchar(100) - > );
让我们看看创建基于函数的索引的语法。
create index index_name on yourTableName (column_name(IntegerSize));
这是查询。
mysql> create index indFirstName on FunctionIndexDemo (FirstName(6)); Records: 0 Duplicates: 0 Warnings: 0
检查索引是否存在。
mysql> SHOW INDEX FROM FunctionIndexDemo;
这是输出。
+-------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | 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)