T-SQL Tutorial

Msg 1505 Level 16 - Create unique index - Duplicate key


On Transact SQL language the Msg 1505 Level 16 - The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name.

Msg 1505 Level 16 Example:

USE tempdb;
GO
CREATE TABLE dbo.DEPARTMENTS (
   id INT NOT NULL,
   name VARCHAR (50) NULL
);
GO
INSERT INTO dbo.DEPARTMENTS (ID, NAME) VALUES (10, N'd1');
INSERT INTO dbo.DEPARTMENTS (ID, NAME) VALUES (20, N'd2');
INSERT INTO dbo.DEPARTMENTS (ID, NAME) VALUES (10, N'd3');
GO

USE tempdb;
GO
ALTER TABLE dbo.DEPARTMENTS
ADD CONSTRAINT PK_ID_DEP PRIMARY KEY CLUSTERED (ID);
GO

Message
Msg 1505, Level 16, State 1, Line 3
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.DEPARTMENTS' and the index name 'PK_ID_DEP'. The duplicate key value is (10).
Msg 1750, Level 16, State 1, Line 3
Could not create constraint or index. See previous errors.
The statement has been terminated.




Solution:

Remove the duplicate key value and then run the alter command.
In my example the duplicate key value is (10).

Other error messages: