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

5

66

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?

Nazario answered 1/2, 2010 at 23:28 Comment(1)
Had this same problem when making a migration that runs when someone updates. i resolved this by first checking for duplicates and deleting them, then checking if the INDEX exists and if not creating it.Conflation
S
120

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.

Syngamy answered 1/2, 2010 at 23:31 Comment(1)
This is the 3 time I've googled this error and ended up here so thanks. Also For future reference I'm adding code to delete duplicates. Hope this is ok DELETE FROM DimMeasureTopic WHERE ID NOT IN ( SELECT MAX(ID) FROM DimMeasureTopic GROUP BY TopicShortName)Applaud
V
16

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

SELECT TopicShortName, COUNT(*)
FROM DimMeasureTopic
GROUP BY TopicShortName
HAVING COUNT(*) > 1
Vicarage answered 1/2, 2010 at 23:32 Comment(0)
K
8

It's because you have records in the table already that are not unique (by the sounds of it, 2 records with a blank value in the TopicShortName field).

So, it's to do with the data, not the index itself.

Klimesh answered 1/2, 2010 at 23:33 Comment(0)
F
5

If you are using code-based migrations, and you rename a property of an entity and you are having an unique index for the property, entity framework will create a new column and trying to add an unique index for the new column but the new column has all null values, therefore it will fail. You need to manually modify the migration code to copy the data from old column before the line to create index.

Fungosity answered 30/10, 2015 at 19:10 Comment(0)
N
0

It should have specified the duplicate key value in the error message. "The duplicate key value is (' ', ' ', ' ') The statement has been terminated. You have duplicate values that need to be addressed.

Nifty answered 19/6, 2019 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.