Apply Unique constraints on existing database

  • I want to apply unique constraints on existing database,but column accepts null values how can i set unique constraints on that column without lose of data. helps me out

  • I assume that you meant that the table should have many records with null value in this column, but the rest of the values should be unique. If I'm correct then you can try and use filtered unique index on that table (the filter would be of course on the null value).

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Yes i need this thanks for your help

  • How can i do it with sql server 2005

  • With a hell of a lot of difficulty (and a computed column). The filtered index is 2008 and above, which since this was posted in the SQL 2008 forum we assumed you were using.

    For 2005, define a computed column on ISNULL(<column that you want to be unique>,<the primary key column of the table>) and put a unique index on that computed column.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Can i apply this approach for column which is varchar type?

  • Yes.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (2/17/2014)


    Yes.

    It's easier with varchar, nvarchar, and varbinary since it's generally not too difficult to convert the primary key to one of those types; other column types could be an issue.

    And there's another complication - you need some way of ensuring that the value you convery the primary key into when the column in question has a null never clashes with some other value in that column, which may turn out to be nontrivial.

    Tom

  • I am creating CLUSTERED INDEX by running below query on sql server 2005

    CREATE TABLE dbo.Example

    (

    col1 varchar(Max) NULL,

    );

    GO

    CREATE VIEW dbo.ExampleUnique

    WITH SCHEMABINDING AS

    SELECT e.col1

    FROM dbo.Example AS e

    WHERE e.col1 IS NOT NULL;

    GO

    CREATE UNIQUE CLUSTERED INDEX cuq

    ON dbo.ExampleUnique (col1);

    GO

    It shows error: Column 'col1' in table 'dbo.ExampleUnique' is of a type that is invalid for use as a key column in an index.

    Any suggestions

  • Previous section is works fine when 'col1 int NULL'

  • ashu.sajwan (2/17/2014)


    ...

    col1 varchar(Max) NULL,

    ...

    CREATE UNIQUE CLUSTERED INDEX cuq

    ON dbo.ExampleUnique (col1);

    It shows error: Column 'col1' in table 'dbo.ExampleUnique' is of a type that is invalid for use as a key column in an index.

    Any suggestions

    The reason for the failure is that columns of lob type (nvarchar(max), varchar(max), varbinary(max), ntext, text, image) can't be key columns in an index. So for example varchar(768) is OK, but varchar(MAX) isn't.

    Incidentally, I think this is a very good technique given that your version of SQL Server doesn't support filtered indexes.

    Tom

  • Your suggestion works for me

    Thanks Tom

  • Why clustered? This isn't typically where you would put the clustered index.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Can you please share other approach to do this easiest way in sql server 2005?

  • I already did.

    GilaMonster (2/17/2014)


    For 2005, define a computed column on ISNULL(<column that you want to be unique>,<the primary key column of the table>) and put a unique index on that computed column.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic. Login to reply