Indexes-Indexing is way to sort and search records in the table. It will improve the speed of locating and retrieval of records from the table.It can be compared with the index which we use in the book to search a particular record.
In Sql Server there are two types of Index
1) Clustered Index
2) Non Clustered Index
How to create Index in the table:-
Non Clustered Index:-
Suppose we have a table tbl_Student and the Sql script for creating this table is given below:-
Suppose it contains the following data-
Now First we can check whether the table contains any index or not.For this we can use the following queries:
sp_helpindex tbl_student
select name from sysindexes where id=object_id('tbl_student')
Now, since we don't make any index on the table tbl_student yet, so when we run the sql statement "sp_helpindex tbl_student" , it will give us the following result:-
The object 'tbl_student' does not have any indexes, or you do not have permissions.
Now , we can make the index on this table by using the following Sql Statement:-
How to create Index in the table:-
Non Clustered Index:-
Suppose we have a table tbl_Student and the Sql script for creating this table is given below:-
CREATE TABLE tbl_Student
(
[StudentId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](150) ,
[LastName] [nvarchar](150),
[Phone] [nvarchar](20),
[Email] [nvarchar](50),
[StudentAddress] [nvarchar](200),
[RegistrationDate] [datetime],
[Enddate] [datetime]
)
(
[StudentId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](150) ,
[LastName] [nvarchar](150),
[Phone] [nvarchar](20),
[Email] [nvarchar](50),
[StudentAddress] [nvarchar](200),
[RegistrationDate] [datetime],
[Enddate] [datetime]
)
Suppose it contains the following data-
Now First we can check whether the table contains any index or not.For this we can use the following queries:
sp_helpindex tbl_student
select name from sysindexes where id=object_id('tbl_student')
Now, since we don't make any index on the table tbl_student yet, so when we run the sql statement "sp_helpindex tbl_student" , it will give us the following result:-
The object 'tbl_student' does not have any indexes, or you do not have permissions.
Now , we can make the index on this table by using the following Sql Statement:-
Create Index (Index name) ON (Table name)(Column Name)
For Example,



Subscribe to this blog
Briefcase
Print



No comments.