12

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
  • 66,506
  • 23
  • 132
  • 141
sharptooth
  • 163,328
  • 92
  • 501
  • 942

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
  • 281,117
  • 39
  • 423
  • 553
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
  • 4,552
  • 3
  • 19
  • 37
Stephan
  • 7,847
  • 3
  • 34
  • 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
  • 66,506
  • 23
  • 132
  • 141