Wednesday, December 15, 2010

How Create a Unique Constraints in SQL Server 2008

Create Unique Constraints Syntax and example.

You can do this with a filtered index in SQL 2008:
Syntax..................
CREATE UNIQUE NONCLUSTERED INDEX idx_col1 ON dbo.MyTable(col1) WHERE col1 IS NOT NULL;

Example.................
CREATE TABLE [dbo].[sav_acc_mcg](
    [acc_id] [bigint] NOT NULL,
    [br_id] [nvarchar](10) NOT NULL,
    [scheme_id] [nvarchar](50) NOT NULL,
    [acc_no_alias] [nvarchar](50) NOT NULL,
    [acc_no] [nvarchar](50) NOT NULL  
);

CREATE UNIQUE NONCLUSTERED INDEX idx_unique_acc_no_alias ON dbo.sav_acc_mcg(acc_no_alias) WHERE acc_no_alias IS NOT NULL;

Run above query in SQL Server 2008
Which will add the unique constrain in the column called "acc_no_alias" on the table name "sav_acc_mcg"

No comments:

Post a Comment