Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server add index without dropping table

Tags:

sql

sql-server

I need to add an index to a table that already has the primary key indexed. The new index will be on another column.

I don't have enough permissions on this table to select 'Alter' by right clicking and selecting 'Script table as'.

Is there any way to add an index to a column without effecting any of the existing data?

myTable:

  • id -- int -----> this is an identity column, set as PK and indexable
  • bookid ---> varchar(20) ----> this is the column that needs index created
like image 608
Roger Dodger Avatar asked Sep 26 '16 19:09

Roger Dodger


People also ask

Does adding an index lock the table?

Yes you can. It will lock the table you're adding an index to while it's being created. If the table is large, it may take awhile as it has to read each row while building the index.

Does dropping a table drop the index SQL Server?

Dropping a table removes the table definition from the data dictionary. All rows of the table are no longer accessible. All indexes and triggers associated with a table are dropped.

Will dropping table drop indexes?

DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified.


1 Answers

You can execute create index script as below, which is similar to what Igor mentioned

CREATE NONCLUSTERED INDEX IX_YourTable ON dbo.YourTable (bookid)
like image 91
Kannan Kandasamy Avatar answered Sep 29 '22 06:09

Kannan Kandasamy