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 2008 Forums
 SQL Server Administration (2008)
 unique key to existing column

Author  Topic 

iceice
Starting Member

5 Posts

Posted - 2012-05-02 : 10:04:32
table has primary key column and "orderid" nvarchar(30) column. table has many records. "orderid" column must be unique.
how to set this column as unique?

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-02 : 10:35:38
If it's set at the primary key it's already unique.
Go to Top of Page

iceice
Starting Member

5 Posts

Posted - 2012-05-02 : 10:46:05
primary key column and "orderid" column are different. "orderid" column has null values too.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-02 : 11:59:56
Ah, misread the question. You can't put a unique constraint on orderid if it has multiple nulls. You either have to update them to be unique, delete them from the table, or create a unique filtered index:
CREATE UNIQUE INDEX ix_UNQ_orderID ON myTable(orderid) WHERE orderid IS NOT NULL
This will work on SQL Server 2008 or higher.
Go to Top of Page
   

- Advertisement -