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

È possibile avere un indice basato su funzione in MySQL?

In versioni di MySQL inferiori a 5.6, non è possibile utilizzare indici basati su funzione. Prima di tutto, per creare un indice basato su funzione in MySQL, creeremo una tabella.

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

Vediamo la sintassi per creare un indice basato su una funzione.

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

Questo è la query.

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

Controlla se l'indice esiste.

mysql> SHOW INDEX FROM FunctionIndexDemo;

Questo è l'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)