🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will focus on the DROP INDEX statement in SQL. Dropping an index can be necessary when it is no longer needed or if it is affecting the performance of write operations. This chapter will cover the definition, syntax, and provide examples to help you understand how to use the DROP INDEX statement effectively.
What is DROP INDEX?
The DROP INDEX statement is used to remove an existing index from a table. Removing an index can help improve the performance of write operations such as INSERT, UPDATE, and DELETE by eliminating the overhead of maintaining the index.
Syntax for DROP INDEX
MySQL
DROP INDEX index_name ON table_name;
SQL Server
DROP INDEX table_name.index_name;
PostgreSQL
DROP INDEX index_name;
Step-by-Step Example
Sample Table and Indexes
First, let's create a sample table named employees and add some indexes to it.
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
department_id INT,
salary DECIMAL(10, 2)
);
Inserting Sample Data
INSERT INTO employees (first_name, last_name, email, department_id, salary)
VALUES
('Ramesh', 'Kumar', 'ramesh.kumar@example.com', 1, 50000),
('Sita', 'Patel', 'sita.patel@example.com', 2, 60000),
('Arjun', 'Singh', 'arjun.singh@example.com', 1, 55000),
('Priya', 'Sharma', 'priya.sharma@example.com', 2, 62000),
('Ravi', 'Verma', 'ravi.verma@example.com', 3, 50000);
Creating Indexes
To create some indexes on the last_name and email columns:
CREATE INDEX idx_last_name
ON employees (last_name);
CREATE UNIQUE INDEX idx_email
ON employees (email);
Dropping an Index
MySQL Example
To drop the idx_last_name index on the employees table:
DROP INDEX idx_last_name ON employees;
SQL Server Example
To drop the idx_last_name index on the employees table:
DROP INDEX employees.idx_last_name;
PostgreSQL Example
To drop the idx_last_name index:
DROP INDEX idx_last_name;
Verifying the Drop
To verify that the index has been dropped, you can check the indexes on the table.
MySQL
SHOW INDEX FROM employees;
SQL Server
EXEC sp_helpindex 'employees';
PostgreSQL
SELECT * FROM pg_indexes WHERE tablename = 'employees';
Example of Dropping a Unique Index
To drop the idx_email unique index on the employees table:
MySQL
DROP INDEX idx_email ON employees;
SQL Server
DROP INDEX employees.idx_email;
PostgreSQL
DROP INDEX idx_email;
Conclusion
The DROP INDEX statement is a straightforward and effective way to remove indexes from a SQL database. This chapter covered the basic syntax for dropping indexes, provided examples to illustrate its use, and demonstrated how to verify the removal of indexes. Understanding how to use DROP INDEX effectively will help you manage and maintain your database schema.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment