Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I allowed to have two indices with the same name?

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?

like image 837
sharptooth Avatar asked May 20 '13 09:05

sharptooth


2 Answers

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.

like image 196
Remus Rusanu Avatar answered Sep 20 '22 05:09

Remus Rusanu


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

like image 23
Darren Avatar answered Sep 20 '22 05:09

Darren