×

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

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

2 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 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 Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 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 DAY() function

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

2 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute 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 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 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 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 Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 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 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 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 LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 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.