CHECK CONSTRAINT on multiple columns

后端 未结 3 1076
忘掉有多难
忘掉有多难 2020-11-30 07:36

I use SQL Server 2008

I use a CHECK CONSTRAINT on multiple columns in the same table to try to validate data input.

I receive an error:

3条回答
  •  悲&欢浪女
    2020-11-30 08:20

    Yes, define the CHECK CONSTRAINT at the table level

    CREATE TABLE foo (
       bar int NOT NULL, 
       fred varchar(50) NOT NULL,
    
       CONSTRAINT CK_foo_stuff CHECK (bar = 1 AND fred ='fish')
    )
    

    You are declaring it inline as a column constraint

    ...
    fred varchar(50) NOT NULL CONSTRAINT CK_foo_fred CHECK (...)
    ...
    

    Edit, easier to post than describe. Fixed your commas.

    CREATE TABLE dbo.Test 
    (   
      EffectiveStartDate  dateTime2(2)        NOT NULL,
      EffectiveEndDate    dateTime2(2)        NOT NULL,  --need comma
      CONSTRAINT CK_CmsSponsoredContents_EffectiveEndDate CHECK (EffectiveEndDate > EffectiveStartDate) --no comma
    );
    

    Of course, the question remains are you using a CHECK constraint where it should be an FK constraint...?

提交回复
热议问题