SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 unique key to existing column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

iceice
Starting Member

5 Posts

Posted - 05/02/2012 :  10:04:32  Show Profile  Reply with Quote
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

USA
15558 Posts

Posted - 05/02/2012 :  10:35:38  Show Profile  Visit robvolk's Homepage  Reply with Quote
If it's set at the primary key it's already unique.
Go to Top of Page

iceice
Starting Member

5 Posts

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

robvolk
Most Valuable Yak

USA
15558 Posts

Posted - 05/02/2012 :  11:59:56  Show Profile  Visit robvolk's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000