×

MySQL Prefix index

Prefix index

This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as the primary and secondary keys. MySQL secondary index stores value in a different data structure. It takes more memory and space on the disk. As a result, the system slows down the data operation.

Syntax

The length type decides the data type for an index or column, and the number of characters decides the non-binary data type. The CHAR, VARCHAR, and TEXT data type column decides length based on the characters. The number of bytes decides the binary data type. The BINARY, VARBINARY, and BLOB data type column decides length based on the bytes. 

The basic syntax of the prefix index shows below.

MySQL EXPRESSION column name (length)

The syntax of the prefix index with the CREATE TABLE command is shown below.

 CREATE TABLE tbl_name (
 Column1 INT NOT NULL PRIMARY KEY,
 Column2 INT NOT NULL,
 Column3 INT NOT NULL,
 Column4 varchar(45),
 Index (column name (length))
 ); 

The syntax of the prefix index into the existing table is shown below.

 CREATE INDEX name
 ON table name (column name (length)); 

The prefix index must use the column prefix key parts for BLOB and TEXT columns. If you use CHAR, VARCHAR, BINARY, and VARBINARY index, then it is optional to create column prefix key parts.

The prefix length depends on the MySQL storage engine. For example, the InnoDB storage engine supports a length of up to 767 bytes. The MyISAM storage engine supports a length of up to 1,000 bytes.

Examples of the prefix index

1) Example: The basic prefix index example shows below.

Execute the below query to create a new table and indexes. Here, the name column createsan index with length. The primary key must be assigned as an index. You create either a single or multiple prefix index in a single table.

 mysql> create table index_table(
 roll_number INT NOT NULL PRIMARY KEY,
 name VARCHAR(10) NOT NULL,
 mark text NOT NULL,
 img blob NOT NULL,
 INDEX (roll_number, name(10))); 

OUTPUT

Execute the following query to get the index of the table.

mysql> show index from index_table;
MySQL: Prefix index

The above output image shows indexes from the table. It contains the key name, column name, index type, and visibility of the table.

2) Example: The primary prefix index on the existing table example shows below.

Execute the below query to create an index with length. The column assigns TEXT data type on a table.

mysql> create indexmark ON index_table(mark (100));

OUTPUT

Execute the following query to get the index of the table.

mysql> show index from index_table;
MySQL: Prefix index

The above output image shows indexes from the table. It contains the key name, column name, index type, and visibility of the table.

3) Example: The prefix index on the existing table shows below.

Execute the below query to create an index with length. The column assigns BLOB data type on a table.

mysql> create indeximg ON index_table(img (400));

OUTPUT

Execute the following query to get the index of the table.

mysql> show index from index_table;
MySQL: Prefix index

The above output image shows indexes from the table. It contains the key name, column name, index type, and visibility of the table.

4) Example: The basic prefix index with database example shows below.

Execute the below query to create an index with length. The column assigns TEXT data type on a table.

mysql> create indexmark ON tutorial.index_table(mark (200));

OUTPUT

Execute the following query to get the index of the table.

mysql> show index from index_table;
MySQL: Prefix index

The above output image shows indexes from the table. It contains the key name, column name, index type, and visibility of the table.


Related Topics

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.