Introduction
Before we start this, do you know what indexes are?
You must have used indexes in a book! To search any topic or chapter, we search that topic in the indexes of the book, find the page number of that topic, and go directly to that topic without checking every page before it. Just imagine this, how difficult it would be if we don't have indexes, and we have to search every page before getting to that desired topic 😱!
Indexes in SQL works like book indexes. If we don't have indexes in SQL, searching the data would be challenging and time-consuming, decreasing the performance of the database.
Read About - Specialization and Generalization in DBMS and Recursive Relationship in DBMS
Do you know, What is an Index?
An index is a key built from one or more than one column in the database, which speeds up fetching or searching rows from the table (or data from the database).
Types of Indexes
Some of the types of indexes in SQL
are:
- Clustered Index
- Non-clustered Index
- Unique Index
- Column Store Index
- Full-text Index
- XML Index
- Index with included columns etc.
Among these, Clustered and Non clustered indexes are the majorly used ones, Now we will learn about Clustered and Non clustered indexes in detail.
Recommended Topic, Schema in DBMS and Checkpoint in DBMS.
Clustered Index
It is a type of index which sorts the data rows in the table according to their key values. In the database, there exists only one clustered index per table.
A clustered index defines the order in which data is stored in the table, sorted only in one way, either increasing or decreasing.
Example of Clustered index
Let us understand it using an example.
Suppose we have a table USER_INFO which contains UserID as a primary key that is self-created. On the basis of this primary key, the USER_INFO table will be sorted as per UserID.
The clustered index is like a dictionary, where sorting order is alphabetical, so a separate index is not needed anymore.
Below is the syntax for creating the above USER_INFO table:
|
CREATE TABLE USER_INFO( UserID int(10) primary key, UserName varchar(20), Location varchar(20), ); Insert into USER_INFO values(105, ‘Ayush Agarwal’ , ‘Lucknow’); Insert into USER_INFO values(102, ‘Neha Singh’, ‘Pune’); Insert into USER_INFO values(106, ‘Aman Kumar’, ‘Delhi’); Insert into USER_INFO values(104, ‘Sumit Pal’, ‘Pune’); Insert into USER_INFO values(107, ‘Divyansh’, ‘Delhi’); Insert into USER_INFO values(103, ‘Shreyanshi’, ‘Kanpur’); Insert into USER_INFO values(101, ‘Vivek Goswami’, ‘Noida’);
SELECT * From USER_INFO; |
The output table “USER_INFO” for the above query will be:
UserID |
UserName |
Location |
101 |
Vivek Goswami |
Noida |
102 |
Neha Singh |
Pune |
103 |
Shreyanshi |
Kanpur |
104 |
Sumit Pal |
Pune |
105 |
Ayush Agarwal |
Lucknow |
106 |
Aman Kumar |
Delhi |
107 |
Divyansh |
Delhi |
To create a clustered index on the other column, we must first remove the primary key to remove the previous index.
Note: Defining a column as a primary key makes that column the table’s clustered index.
Below is the syntax for removing the previous index to make any other column the clustered index.
|
Drop index table_name.index_name; Create Clustered index IX_table_name_column_name on table_name (column_name ASC); |
This is the general syntax to create a clustered index from any other column.
Must Read SQL Clauses




