×

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 FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL RADIANS() function

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

2 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.

MySQL LENGTH() Function

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

2 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 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 Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 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 Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples. Introduction of MySQL QUARTER() function The QUARTER() function in MySQL returns the...

3 minutes read.