The Wayback Machine - https://web.archive.org/web/20241005222525/https://www.geeksforgeeks.org/sql-drop-index/
Open In App

SQL DROP INDEX Statement

Last Updated : 28 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL DROP INDEX statement removes an existing Index from a database table.

SQL DROP INDEX

The SQL DROP INDEX Command is used to remove an index from the table. Indexes occupy space, which can cause extra time consumption on table modification operations.

Benefits of Using DROP INDEX:

  • Removing unused indexes can improve INSERT, UPDATE, and DELETE operations on the table.
  • It will also free up some space.
  • Deleting an index can have a significant impact on database queries therefore only drop the unused or unrequired indexes.

Note: Indexes created by PRIMARY KEY or UNIQUE constraint can not be deleted with just a DROP INDEX statement. To delete such indexes, we need to first drop the constraints using the ALTER TABLE statement, and then drop the index.

Syntax

DROP INDEX Syntax differs in different database systems.

MySQL

ALTER TABLE table_name DROP INDEX index_name;

MS Access

DROP INDEX index_name ON table_name;

SQL Server

DROP INDEX table_name.index_name;

DB2/Oracle

 DROP INDEX index_name;

PostgreSQL

DROP INDEX index_name;

SQL DROP INDEX Example

Let’s look at some examples of how to drop an index in SQL.

First, let’s create a table and add an index using the CREATE INDEX Statement. We will be using SQL database in the examples. 

MySQL
CREATE DATABASE GEEKSFORGEEKS;
USE GEEKSFORGEEKS;
 CREATE TABLE EMPLOYEE(
   EMP_ID INT,
   EMP_NAME VARCHAR(20),
   AGE INT,
   DOB DATE,
   SALARY DECIMAL(7,2)); 
 CREATE INDEX EMP
 ON EMPLOYEE(EMP_ID,EMP_NAME); 

Output:

Creating an index on two columns

Creating an index on two columns

Now let’s look at some examples of DROP INDEX statement and understand its workings in SQL. We will learn different use cases of the SQL DROP INDEX statement with examples.

We can drop the index using two ways either with IF EXISTS or with ALTER TABLE so we will first drop the index using if exists.

SQL DROP INDEX with IF EXISTS Example

Removing an index using SQL DROP INDEX statement with IF EXISTS clause, allows the user to remove the index only if it exists in the table.

Query:

DROP INDEX IF EXISTS EMP ON EMPLOYEE;
Dropping index

Dropping index

Output

Since there are no indexes in the database with the supplied name, the aforementioned query simply ends execution without returning any errors.

Commands Excuted Successfully;

SQL DROP index with ALTER TABLE Example

Query:

 ALTER TABLE EMPLOYEE
DROP INDEX EMP;

Output

Dropping the index

Dropping the index

Verify DROP INDEX

To verify if the DROP INDEX statement has successfully removed the index from the table, we can check the indexes on the table. If the index is not present in the list, we know it has been deleted.

Syntax

The syntax for viewing the index on a table differs for different databases, for example:

SQL Server:

SELECT * FROM sys.indexes WHERE object_id = (SELECT object_id FROM sys.objects WHERE name = 'YOUR_TABLE_NAME')

MySQL:

SHOW INDEXES FROM YOUR_TABLE_NAME;

PostgreSQL:

SELECT * FROM USER_INDEXES;

Oracle:

SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'your_table_name';

Important Points About SQL DROP INDEX Statement

  • The SQL DROP INDEX statement is used to remove an existing index from a database table.
  • It optimizes database performance by reducing index maintenance overhead.
  • It improves the speed of improve INSERT, UPDATE, and DELETE operations on the table.
  • Indexes created by PRIMARY KEY or UNIQUE constraints cannot be dropped using just the DROP INDEX statement.
  • The IF EXISTS clause can be used to conditionally drop the index only if it exists.
  • To verify if the DROP INDEX statement has successfully removed the index from the table, you can check the indexes on the table.

SQL DROP INDEX Statement – FAQs

How to create an index in SQL?

To create an index in MYSQL we use the CREATE INDEX command.

How to drop an index in SQL?

To drop an index in SQL we use the ALTER TABLE DROP INDEX command.

What is the need to drop an index?

Generally we drop an index and then recreate it because it increases the data insertion speed.



Previous Article
Next Article

Similar Reads

CREATE and DROP INDEX Statement in SQL
The CREATE INDEX statement in SQL is a powerful tool used to enhance the efficiency of data retrieval operations by creating indexes on one or more columns of a table. Indexes are crucial database objects that significantly speed up query performance, especially when dealing with large datasets. In this article, We will learn about How to CREATE an
4 min read
SQL CREATE INDEX Statement
The CREATE INDEX statement in SQL is a powerful tool designed to enhance the performance of data retrieval operations. By creating indexes on tables, databases can quickly locate and retrieve data, significantly speeding up query execution. In this article, We will learn about the SQL CREATE INDEX Statement by understanding various examples in deta
4 min read
Drop login in SQL Server
The DROP LOGIN statement could be used to delete an user or login that used to connect to a SQL Server instance. Syntax - DROP LOGIN loginname; GO Permissions : The user which is deleting the login must have ALTER ANY LOGIN permission on the server. Note - A login which is currently logged into SQL Server cannot be dropped. If you try to drop login
1 min read
DROP SCHEMA in SQL Server
The DROP SCHEMA statement could be used to delete a schema from a database. SQL Server have some built-in schema, for example : dbo, guest, sys, and INFORMATION_SCHEMA which cannot be deleted. Syntax : DROP SCHEMA [IF EXISTS] schema_name; Note : Delete all objects from the schema before dropping the schema. If the schema have any object in it, the
1 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, consider the below table 'Employee'. Create a Table: C
2 min read
SQL DROP CONSTRAINT
In SQL, the DROP CONSTRAINT command is used to remove constraints from the columns of a table. Constraints are used to limit a column on what values it can take. We add constraints to our columns while we are creating the structure of tables in SQL and when we are actually inserting values in those tables sometimes we find that there is no further
3 min read
SQL DROP COLUMN
The Data Definition Language (DDL) command DROP COLUMN is used in SQL (Structured Query Language). The structure of database objects like tables can be defined, altered, or deleted using DDL statements in SQL. A column can be removed from a database table using the DROP COLUMN command. The table is altered permanently by this command. There is no g
3 min read
DROP and TRUNCATE in SQL
DROP and TRUNCATE in SQL remove data from the table. The main difference between DROP and TRUNCATE commands in SQL is that DROP removes the table or database completely, while TRUNCATE only removes the data, preserving the table structure. Let's understand both these SQL commands in detail below: What is DROP Command?DROP command in SQL is used to
4 min read
Create, Alter and Drop schema in MS SQL Server
Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to create, alter, and drop the schema. What is Schema?A
4 min read
SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a table using the ALTER TABLE command and DROP CONS
3 min read
Difference between DROP and TRUNCATE in SQL
In SQL, the DROP and TRUNCATE commands are used to remove data, but they operate differently. DROP deletes an entire table and its structure, while TRUNCATE removing only the table data. Understanding their differences is important for effective database management. In this article, We will learn about the difference between DROP and TRUNCATE in SQ
3 min read
SQL DROP TABLE
The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making it unrecoverable unless backed up. In this article
3 min read
SQL DROP DATABASE
SQL DROP DATABASE statement is a powerful command used to permanently delete an existing database from a database management system (DBMS). In this article, We will learn about SQL DROP DATABASE in detail by understanding various examples in detail. SQL DROP DATABASESQL DROP DATABASE statement is used to delete an existing database from the databas
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Reverse Statement Word by Word in SQL server
To reverse any statement Word by Word in SQL server we could use the SUBSTRING function which allows us to extract and display the part of a string. Pre-requisite :SUBSTRING function Approach : Declared three variables (@Input, @Output, @Length) using the DECLARE statement. Use the WHILE Loop to iterate every character present in the @Input. For th
2 min read
How to Update Multiple Columns in Single Update Statement in SQL?
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required. UPDATE for Multiple ColumnsSyntax: U
3 min read
Multi-Statement Table Valued Function in SQL Server
In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. Multi-statement function is very much similar to inline functions only difference is that in multi-statement function we
4 min read
SQL | INSERT IGNORE Statement
We know that a primary key of a table cannot be duplicated. For instance, the roll number of a student in the student table must always be distinct. Similarly, the EmployeeID is expected to be unique in an employee table. When we try to insert a tuple into a table where the primary key is repeated, it results in an error. However, with the INSERT I
2 min read
SQL | DESCRIBE Statement
Prerequisite: SQL Create Clause As the name suggests, DESCRIBE is used to describe something. Since in a database, we have tables, that's why do we use DESCRIBE or DESC(both are the same) commands to describe the structure of a table. Syntax: DESCRIBE one; OR DESC one; Note: We can use either DESCRIBE or DESC(both are Case Insensitive). Suppose our
2 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records to the specified table. We can use this statement to add data directly to the table. We use the VALUES keyword along with the INSERT INTO statement. VALUES keyword is accompanied by column names in a specific order in which we want to insert values in them. SELECT statement is used
5 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or many underlying tables. Views help simplify complex
4 min read
How to Combine LIKE and IN in SQL Statement
Combining 'LIKE' and 'IN' operators in SQL can greatly enhance users' ability to retrieve specific data from a database. By Learning how to use these operators together users can perform more complex queries and efficiently filter data based on multiple criteria, making queries more precise and targeted. This article explores how the LIKE and IN op
4 min read
SQL MERGE Statement
SQL MERGE Statement combines INSERT, DELETE, and UPDATE statements into one single query. MERGE Statement in SQLMERGE statement in SQL is used to perform insert, update, and delete operations on a target table based on the results of JOIN with a source table. This allows users to synchronize two tables by performing operations on one table based on
4 min read
SQL SELECT INTO Statement
The SQL SELECT INTO statement is used to copy data from one table into a new table. Note: The queries are executed in SQL Server, and they may not work in many online SQL editors so better use an offline editor. SyntaxSQL INSERT INTO Syntax is: SELECT column1, column2... INTO NEW_TABLE from SOURCE_TABLEWHERE Condition; To copy the entire data of th
3 min read
How to Update Two Tables in One Statement in SQL Server?
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server. SyntaxUpdating two tables in one statement in SQL
3 min read
DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table. SyntaxMS SQL Server DELETE statement syntax is: DELETE FROM table_name WHERE condition; Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclude the WHERE clause, all records in the table will
1 min read
SQL UPDATE Statement
SQL UPDATE Statement modifies the existing data from the table. UPDATE Statement in SQL The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement. In a very simple way, we can say that SQL commands(UPDATE and
3 min read
SQL Server MERGE Statement
SQL Server MERGE statement combines INSERT, UPDATE, and DELETE operations into a single transaction. MERGE in SQL ServerThe MERGE statement in SQL provides a convenient way to perform INSERT, UPDATE, and DELETE operations together, which helps handle the large running databases. But unlike INSERT, UPDATE, and DELETE statements MERGE statement requi
2 min read
SQL SELECT IN Statement
SQL SELECT IN Statement allows to specify multiple values in the WHERE clause. It is similar to using multiple OR conditions. It is particularly useful for filtering records based on a list of values or the results of a subquery. The IN operator compares a value with a set of values, and it returns a TRUE if the value belongs to that given set, els
3 min read
Article Tags :