Indexes are created on column(s) of tables or
An index is a hierarchy of nodes organized in a
In the SQL server, indexes are divided into two categories:
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.
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
Person
table in SQL.Id
column of type integer. Name
column of type varchar
.Gender
column of type varchar
.Person
table using the Insert
statement.Person
table.Note: The output of the code above will produce results in no particular order.
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.
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
Person
table in SQL and insert records into the table using the Insert
statement.Id
column.Person
table.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
.
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).
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'
Person
table in SQL and insert record into the table using the Insert
statement.Name
column.Name
column. When a
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';
Person
table in SQL and insert records into the table using the Insert
statement.Name
column.Name
column. Note: In this example, when unique key constraint is added, non-clustered index is automatically created.