×

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 column order in the table.

Suppose you want to display the index in order as per your requirement. The index order assigns either ascending or descending. You will use the "DESC" keyword for descending order. If you want your index in ascending order, then use the "ASC" keyword.

Syntax

The basic syntax of the descending index shows below.

INDEX key_name (column1 DESC, column DESC)

MySQL descending index shows below.

 CREATE TABLE table_name
 (Column1 INT NOT NULL,
 Column INT NOT NULL,
 INDEX key_name (column1 DESC, column DESC));
 MySQL ascending index shows below.
 CREATE TABLE table_name
 (Column1 INT NOT NULL,
 Column INT NOT NULL,
 INDEX key_name (column1 ASC, column ASC));
 MySQL descending and ascending index shows below.
 CREATE TABLE table_name
 (Column1 INT NOT NULL,
 Column INT NOT NULL,
 INDEX key_name (column1 DESC, column ASC)); 

Examples of the Descending index

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

Execute the below query to get descending orders of the column. Here we will assign the DESC keyword to the phone and name column.

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

OUTPUT

Execute the below query to get the required index and its information.

mysql> show index from index_table;
Descending Index

The above output image shows a similar index name for multiple columns. The collation shows the "D" value, which means descending order for the index.

2) Example: The multiple descending index example shows below.

Execute the below query to get descending orders of the column. All column uses descending order using the "DESC" keyword. The multiple indexes with several columns assign for descending index.

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

OUTPUT

Execute the below query to get the required index and its information.

mysql> show index from index_table;
Descending Index

The above output image shows a single index name for multiple columns. The collation shows the "D" value, which means descending order for the index.

3) Example: The basic ascending index example shows below.

Execute the below query to get descending orders of the column. All column uses order with the "ASC" and "DESC" keyword. The multiple indexes with several table columns assign for ascending and descending index.

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

OUTPUT

Execute the below query to get the required index and its information.

mysql> show index from index_table;
Descending Index

The above output image shows a single index name to multiple columns. The collation shows the "A" and "D" values where "A" displays an ascending order and "D" displays a descending order to the index. The complete table index assigns descending order.

4) Example: The descending and ascending index example shows below.

Execute the below query to get ascending orders of the column. All column uses ascending order using the "ASC" keyword. The multiple indexes with several table columns assign to ascending index.

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

OUTPUT

Execute the below query to get the required index and its information.

mysql> show index from index_table;
Descending Index

The above output image shows a single index name for multiple columns. The collation shows the "A" value, which means ascending order for index. The complete table index assigns ascending order.


Related Topics

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL ELT() function

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

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

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 Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 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 SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL RIGHT() Function

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

3 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 FORMAT() function

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

2 minutes read.

MySQL RPAD() function

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

2 minutes read.