Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create index because of duplicate that doesn't exist?

I'm getting an error running the following Transact-SQL command:

CREATE UNIQUE NONCLUSTERED INDEX IX_TopicShortName ON DimMeasureTopic(TopicShortName) 

The error is:

Msg 1505, Level 16, State 1, Line 1 The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.DimMeasureTopic' and the index name 'IX_TopicShortName'. The duplicate key value is ().

When I run SELECT * FROM sys.indexes WHERE name = 'IX_TopicShortName' or SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[DimMeasureTopic]') the IX_TopicShortName index does not display. So there doesn't appear to be a duplicate.

I have the same schema in another database and can create the index without issues there. Any ideas why it won't create here?

like image 994
Alex Angas Avatar asked Feb 01 '10 23:02

Alex Angas


People also ask

Can we CREATE INDEX on duplicate values?

Yes, you can create a clustered index on key columns that contain duplicate values.

Is index will not allow duplicate values?

You can prevent duplicate values in a field in an Access table by creating a unique index. A unique index is an index that requires that each value of the indexed field is unique.

What is the correct syntax for creating an index?

The syntax to create an index in SQL is: CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, ... column_n); UNIQUE.

How do I create a composite index in SQL?

Composite Indexes A composite index is an index on two or more columns of a table. Its basic syntax is as follows. CREATE INDEX index_name on table_name (column1, column2);


2 Answers

It's not that the index already exists, but that there are duplicate values of the TopicShortName field in the table itself. According to the error message the duplicate value is an empty string (it might just be a facet of posting I guess). Such duplicates prevent the creation of a UNIQUE index.

You could run a query to confirm that you have a duplicate:

SELECT     TopicShortName,     COUNT(*) FROM     DimMeasureTopic GROUP BY     TopicShortName HAVING     COUNT(*) > 1 

Presumably in the other database the data are different, and the duplicates are not present.

like image 181
martin clayton Avatar answered Sep 28 '22 09:09

martin clayton


The duplicate is in your data, try running this query to find it.

SELECT TopicShortName, COUNT(*) FROM DimMeasureTopic GROUP BY TopicShortName HAVING COUNT(*) > 1 
like image 28
Donald Byrd Avatar answered Sep 28 '22 08:09

Donald Byrd