13

I have a large .sqlproj project. In one .sql file I have one table definition:

CREATE TABLE [dbo].[TableOne] (
    [ColumnName] UNIQUEIDENTIFIER NULL
);
GO
CREATE UNIQUE CLUSTERED INDEX [TableOneIndex]
  ON [dbo].[TableOne]([ColumnName] ASC;

In another .sql file I have another table definition:

CREATE TABLE [dbo].[TableTwo] (
    [ColumnName] UNIQUEIDENTIFIER NULL
);
GO
CREATE UNIQUE CLUSTERED INDEX [TableOneIndex]
  ON [dbo].[TableTwo]([ColumnName] ASC;

Note that both indices are called TableOneIndex. Yet the project builds fine and deploys fine.

How can this be legal?

Darren
  • 68,902
  • 24
  • 138
  • 144
sharptooth
  • 167,383
  • 100
  • 513
  • 979

3 Answers3

15

The CREATE INDEX specifications explain this:

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.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
10

Since they are 2 separate tables and TableTwo IS NOT a view of TableOne, it's perfectly normal to have the same index name on both tables

Index name must be UNIQUE within the table NOT the whole database.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
Stephan
  • 8,000
  • 3
  • 36
  • 42
10

They have the same name in the SYS.INDEX tables however they have complete different OBJECT_ID's.

Look at the sys.tables

  SELECT * FROM
  SYS.TABLES
  WHERE NAME LIKE 'TABLE%'

and then do:

  SELECT * FROM SYS.INDEXES 
  WHERE OBJECT_ID IN (245575913
,277576027)

Where the object ID's are the ID's from the sys.tables table relating to TableOne and TableTwo

Darren
  • 68,902
  • 24
  • 138
  • 144