What are unique, clustered, and non-clustered indexes in SQL?
Unique, clustered, and non-clustered indexes in SQL
Indexes are created on column(s) of tables or
Structure of index
An index is a hierarchy of nodes organized in a
Types of indexes
In the SQL server, indexes are divided into two categories:
- Clustered index
- Non-clustered index
In a non-clustered index, a pointer to the actual data is stored at the leaf level. However, actual data is stored at the leaf level of a clustered index.
Code example
create table Person(Id int,Name varchar(50),Gender varchar(1));Insert into Person values(11,'Asma','F');Insert into Person values(3,'Rameen','F');Insert into Person values(77,'Wahab','M');Insert into Person values(38,'Saad','M');select * from Person
Explanation
- Lines 1–5: We create the
Persontable in SQL. - Line 2: We initialize an
Idcolumn of type integer. - Line 3: We initiate a
Namecolumn of typevarchar. - Line 4: We initiate a
Gendercolumn of typevarchar. - Lines 7–10: We insert records into
Persontable using theInsertstatement. - Line 12: We print all records of the
Persontable.
Note: The output of the code above will produce results in no particular order.
Clustered index
When a
Moreover, we can create clustered indexes on both unique and non-unique columns. To create clustered index on a column other than the primary key column, we have to drop the previous index first.
Code example
create table Person(Id int,Name varchar(50),Gender varchar(1));Insert into Person values(11,'Asma','F');Insert into Person values(3,'Rameen','F');Insert into Person values(77,'Wahab','M');Insert into Person values(38,'Saad','M');ALTER TABLE PersonADD PRIMARY KEY (Id);select * from Person
Explanation
- Lines 1–10: We create
Persontable in SQL and insert records into the table using theInsertstatement. - Lines 12–13: We add primary key constraint on the
Idcolumn. - Line 15: We print all records of the
Persontable.
Note: In this example, when the primary key constraint is added, a clustered index is automatically created. The output of this code will produce in increasing order of
Id.
Non-clustered index
A non-clustered index is a logical index and doesn’t order data in a table/view according to the column(s) on which the index is built. They improve the performance of frequently used queries that are not handled by a clustered index. However, a non-clustered index is slower since the first index is searched for pointers of data. Next, a lookup is performed on a clustered index.
Each table/view can have multiple non-clustered indexes. Additionally, a Non-clustered index can be defined on a unique and non-unique column(s).
Code example
create table Person(Id int,Name varchar(50),Gender varchar(1));Insert into Person values(11,'Asma','F');Insert into Person values(3,'Rameen','F');Insert into Person values(77,'Ali','M');Insert into Person values(38,'Wahab','M');create index NON_CLUSTERED_INDEX_NAMEon Person (Name ASC);Select * from Person where Name = 'Wahab'
Explanation
- Lines 1–10: We create a
Persontable in SQL and insert record into the table using theInsertstatement. - Lines 12–13: We create a non-clustered index on the
Namecolumn. - Line 15: We create a non-clustered index to fetch records based on the
Namecolumn.
Unique index
When a
Code example
create table Person(Id int,Name varchar(50),Gender varchar(1));Insert into Person values(11,'Asma','F');Insert into Person values(3,'Rameen','F');Insert into Person values(77,'Ali','M');Insert into Person values(38,'Wahab','M');ALTER TABLE PersonADD Unique KEY (Name);Select * from Person where Name='Asma';
Explanation
- Lines 1–10: We create a
Persontable in SQL and insert records into the table using theInsertstatement. - Lines 12–13: We add non-unique key constraint on the
Namecolumn. - Line 15: We use non-clustered index to fetch records based on the
Namecolumn.
Note: In this example, when unique key constraint is added, non-clustered index is automatically created.