MySQL 中可以有基于函数的索引吗?

mysqlmysqli database

MySQL 5.6 以下版本不支持基于函数的索引。首先,要在 MySQL 中创建基于函数的索引,我们将创建一个表。

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

让我们看看创建基于函数的索引的语法。

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

这是查询。

mysql> create index indFirstName on FunctionIndexDemo (FirstName(6));
Query OK, 0 rows affected (0.56 sec)
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)

相关文章