tags:

views:

61

answers:

3

Do index names have to be unique accross the entire sql server database, or just for that table?

For example, should I name my index: IX_OrderLoadCarrierDelivery_OrderLoadID

for the OrderLoadID column of the OrderLoadCarrierDelivery table. Or should I just name it IX_OrderLoadID

Thanks!

+4  A: 

They have to be unique for the table or view they were created for.

Here is a reference on msdn that details this.

FTA:

index_name

Is the name of the index. Index names must be unique within a table or view but do not have to be unique within a database. Index names must follow the rules of identifiers.

I believe the convention is

IX_FieldName
Joseph
I think you mean IX_TableName_FieldName. You're thinking along the lines of a foreign key relationship.
Bob Kaufman
looks like an FK convention, no?
gbn
@Bob You're right, will edit
Joseph
Unless I'm missing something, in MS-SQL, index names only need to be unique within a given table...? I frequently create "duplicate" names, and indeed just did so, for test purposes (in a MSSQL Express 2005 instance).
mjv
@mjv Oh wow, you're right, I was completely wrong on that account. I'll edit my answer and quote a source for verification!
Joseph
+2  A: 

No, per table.

That is, a unique (object_id, name) column pair in sys.indexes rather then just (name) in sys.objects (ignoring schema_id)

I'd also use something like IX_SingleColumn or IX_ParentTable. Adding table is superfluous unlike a default or check constraint, say, that is unique per DB

gbn
+1 it is per table, indeed. Not sure about all the reps and accepted response stating it is per DB. Are we missing something? sysindexes tables can well have several entries with the same name, provided the id (which relates to the table, in sysobjects) is distinct...
mjv
A: 

They have to be unique as everything gets stored in sysobjects with the name as key If you use SQL management studio, it's IX_Table_Field syntax

Kumar
Wrong on both counts.
gbn