Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
MySQL DROP INDEX Statement
Next article icon

CREATE and DROP INDEX Statement in SQL

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 an important database objects that significantly speed up query performance, especially when dealing with large datasets.

This article will guide us through the CREATE INDEX and DROP INDEX statements in SQL with detailed explanations, practical examples, and outputs. We will also discuss unique indexes, multi-column indexes, and scenarios where removing indexes is necessary.

What is an Index in SQL?

An index in SQL is a database objects that improve the speed of data retrieval operations on a table, making queries more efficient. It works similarly to an index in a book, allowing the database engine to find rows quickly based on the values in indexed columns. Without indexes, the database performs a full table scan to retrieve data, which is inefficient for large tables.

Benefits of Indexes

  • Faster Query Performance: Indexes reduce the time taken to retrieve data by optimizing lookup paths.
  • Efficient Filtering: Speeds up operations like WHERE, JOIN, and ORDER BY clauses.
  • Unique Data Enforcement: Unique indexes ensure no duplicate values in the indexed columns.

CREATE INDEX Statement in SQL

The CREATE INDEX statement in SQL is used to create an index on one or more columns of a table. It can also create unique indexes that enforce the uniqueness of values in the specified columns. They work by providing a quick way to look up rows based on the values in the indexed columns

Syntax:

CREATE INDEX indexname
ON tablename (columnname1, columnname2, ...);

Key Terms

  • index_name: The name of the index.
  • table_name: The name of the table where the index will be created.
  • column_name(s): The column(s) on which the index is built.

Creating a Unique Index

A unique index in SQL ensures that the values in the indexed columns are distinct, preventing duplicate entries. It is commonly used to enforce data integrity by making sure that no two rows in a table have the same value in the specified columns.

Syntax

CREATE UNIQUE INDEX indexname
ON tablename (columnname1, columnname2, ...);

Example 1: Creating an Index on a Single Column

Let’s take an example of a Customer Table where we will create an index on the Country column of the Customer table. This index will help speed up queries that filter or sort based on the Country column, making the data retrieval process more efficient.

1. Create Table Customer

CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);

2. Insert some sample data into the Customers table

INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES
(1, 'Shubham', 'Thakur', 'India', 23, 1234567890),
(2, 'Aman', 'Chopra', 'Australia', 21, 1234567891),
(3, 'Naveen', 'Tulasi', 'Sri Lanka', 24, 1234567892),
(4, 'Aditya', 'Arpan', 'Austria', 21, 1234567893),
(5, 'Nishant', 'Jain', 'Spain', 22, 1234567894);

Output

 

3. Create Index

CREATE INDEX City
ON Customer(Country);

Note: This code does not produce any output directly visible to the user. When an index is created, the database management system updates its internal data structures to include the new index, but this process does not produce any visible output.

Query:

SELECT * FROM sqlite_master WHERE 
type = 'index' AND tbl_name = 'Customer';

Output

 

Example 2: Creating an Index on Multiple Columns

In this example, we will create an index on multiple column Country and Age to improve query performance when filtering or sorting data based on both columns. This composite index allows the database to quickly retrieve rows that match specific combinations of values in the Country and Age columns, optimizing queries that involve both fields.

Syntax

CREATE INDEX idx_Customer
_Country_Age ON Customer (Country, Age);

Example

SELECT * 
FROM Customer
WHERE Country = 'India' AND Age = 23;

Output

CustomerID CustomerName LastName Country Age Phone
1 Shubham Thakur India 23 xxxxxxxxxx

Explanation:

This query filters the Customer table for records where the Country is ‘India’ and the Age is 23, returning the row with CustomerID = 1.

DROP INDEX in SQL

The DROP INDEX statement in SQL is used to remove an existing index from a table. Indexes are database objects that improve the speed of data retrieval operations by providing quick access paths to rows in a table.
However, there are situations where an index may no longer be needed or could be causing performance issues, in which case we might want to drop it.

Syntax

DROP INDEX tablename.indexname;

Query:

DROP INDEX City;

Output

Output2

Explanation:

The index is deleted, and the database reverts to performing full table scans for queries involving the Country column.

Conclusion

The CREATE INDEX statement is an essential tool for optimizing query performance in SQL. Whether we need a standard or unique index, it provides quick lookup paths for efficient data retrieval. However, it’s equally important to understand when to remove unused or inefficient indexes using the DROP INDEX statement. By carefully designing and managing indexes, we can significantly improve the performance and scalability of our database.



Next Article
MySQL DROP INDEX Statement

K

khushboogoyal499
Improve
Article Tags :
  • Databases
  • DBMS
  • SQL
  • DBMS-SQL

Similar Reads

  • SQL CREATE INDEX Statement
    In SQL, indexes are important for optimizing query performance by speeding up data retrieval operations. The CREATE INDEX statement is used to create indexes in tables, enabling quicker searches and improving database efficiency. In this article, we will explain how to use the SQL CREATE INDEX State
    5 min read
  • SQL DROP INDEX Statement
    In SQL, indexes play an essential role in optimizing query performance by speeding up data retrieval operations. However, indexes can also slow down data modification operations such as INSERT, UPDATE, and DELETE due to the overhead of maintaining them. When an index is no longer required or becomes
    5 min read
  • MySQL DROP INDEX Statement
    MySQL, an open-source relational database management system, is widely used for web-based applications. Managing indexes in MySQL is important for maintaining optimal database performance. Indexes improve query speed by allowing quick data retrieval, but sometimes you may need to remove an index to
    3 min read
  • MySQL Create Database Statement
    The MySQL CREATE DATABASE statement is used to create a new database. It allows you to specify the database name and optional settings, such as character set and collation, ensuring the database is ready for storing and managing data. In this article, we are going to learn how we can create database
    4 min read
  • PL/SQL DELETE Statement
    In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
    7 min read
  • PL/SQL CREATE TABLE Statement
    PL/SQL CREATE TABLE statement is a fundamental aspect of database design and allows users to define the structure of new tables, including columns, data types, and constraints. This statement is crucial in organizing data effectively within a database and providing a blueprint for how data should be
    4 min read
  • DROP and TRUNCATE in SQL
    The DROP and TRUNCATE commands in SQL are used to remove data from a table, but they work differently. Understanding the difference between these two commands is important for proper database management, especially when dealing with large amounts of data. This article provides an in-depth explanatio
    5 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 exclu
    1 min read
  • How to Create One Table From Another Table in SQL
    Creating a table based on the structure and data of an existing table is a common task in database management. This process allows us to replicate a table for backup, testing or data transformation purposes. SQL provides efficient methods to create one table from another while preserving the schema,
    3 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 ma
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences