Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 alter column to unique constraint error

Author  Topic 

smh
Yak Posting Veteran

94 Posts

Posted - 2007-11-12 : 23:49:28
Hello:

I followed an example on your forum to alter column to a unique constraint:

alter table [tblOrder]
add constraint PK_OrdNumb unique nonclustered (OrderNumber)

and got error: The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.tblOrder' and the index name 'PK_OrdNumb'. The duplicate key value is (0).

the current column is defined as: OrderNumber] [int] NULL,

There is an identity,primary key in the table as well but this should not be a problem I think.

Thanks for any help
smh

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-11-12 : 23:58:54
It means there is a duplicate record in the OrderNumber field which could be more than 1 NULLs. You are only allowed 1 NULL value as a unique index. If there are more than 1 NULL values in that column, the index will fail.






Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

smh
Yak Posting Veteran

94 Posts

Posted - 2007-11-13 : 00:01:24
Yes, thats it. Actually, I had deleted the field and re inserted it as just a int, null field, and then the alter statement failed. When I inserted unique values into all of the field so that there were no null values, it worked. I must have read your mind.

Thanks so much.
Go to Top of Page
   

- Advertisement -