• 设置唯一性,NULL不唯一,其他都唯一。


    方法一:
    CREATE TABLE [Table] (    [id] [int] IDENTITY (1, 1) NOT NULL ,
       
    [Col] [int] NULL ,
       
    [Col_x] AS ( CASE WHEN Col IS NULL THEN id ELSE 0 END )
       
    CONSTRAINT UNQ_NULLS UNIQUE ( Col,Col_x )
    )
    GOINSERT INTO [Table] VALUES( '1' )
    INSERT INTO [Table] VALUES('2' )
    INSERT INTO [Table] VALUES('3' )
    INSERT INTO [Table] VALUES( NULL )
    INSERT INTO [Table] VALUES( NULL )
    INSERT INTO [Table] VALUES( '1' )
    select * from [table]drop table [table]


    方法二:
    CREATE TABLE [Table] (    [id] [int] IDENTITY (1, 1) NOT NULL ,
       
    [Col] varchar(10) NULL
        )
    GOCREATE VIEW dbo.V_Table
    WITH SCHEMABINDING
    ASSELECT    id, Col
    FROM        dbo.[Table]WHERE    (Col IS NOT NULL)
    GOCREATE UNIQUE CLUSTERED INDEX IDX1 ON V_Table( Col )
    GO

  • 相关阅读:
    Odd sum CodeForces
    Chips CodeForces
    Secrets CodeForces
    Voting CodeForces
    Jury Meeting CodeForces
    Planning CodeForces
    Maxim Buys an Apartment CodeForces
    Chemistry in Berland CodeForces
    Monitor CodeForces
    Four Segments CodeForces
  • 原文地址:https://www.cnblogs.com/aoyihuashao/p/1623744.html
Copyright © 2020-2023  润新知