Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using INDEX in SQL Server

I need to create am index in SQL however it needs to display the records by entering only a part of the name. The index should be created to retrieve the student information using part of the student name (i.e. if the name if Johnanesburg; the user can input John)

I used the syntax below but it wont work

create index Student ON Student(SName)
SELECT * FROM Student WHERE StRegNo LIKE A% 
go
like image 344
Yoosuf Avatar asked Nov 30 '25 17:11

Yoosuf


2 Answers

I think your problem is here: A%

Try wrapping it in apostrophes.

SELECT * 
  FROM Student 
 WHERE StRegNo LIKE 'A%'

Also, you may want a GO statement after you create your index.

like image 50
Neil Knight Avatar answered Dec 02 '25 15:12

Neil Knight


The index you are creating over SName will not provide as much benefit for the select statement you are running as one created over StRegNo. Assuming that StRegNo is the primary key on the Student table you could try:

CREATE CLUSTERED INDEX IX_Student on Student(StRegNo)

SELECT * 
FROM Student 
WHERE StRegNo LIKE 'A%'

However it appears that the SQL you have provided is at odds with your question. If you want to search based on student name then you might want the following instead.

CREATE NONCLUSTERED INDEX IX_Student on Student(SName)

SELECT * 
FROM Student 
WHERE SName LIKE 'A%'
like image 44
Born2BeMild Avatar answered Dec 02 '25 13:12

Born2BeMild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!