create table table1
(
  c1 int,
  c2 int,
  constraint df_c1_c2 check(c2>(case when c1=1 then 5 when c1=2 then 15 end))
)

解决方案 »

  1.   

    我说的是一个例子,可要是复杂的情况怎么办呢?
    比如:
    table1
    c1 int,
    c2 int
    要求的约束是:当c1=1 时 c2>5;当c1=2 时 c2>15;c1=3时c2必须能被5整除,怎么写呢?
      

  2.   

    --创建验证函数
    create function f_check(@c1 int,@c2 int)
    returns int
    as
    begin
          if @c1=1 and @c2<=5
             return 0
     
          if @c1=2 and @c2<15
             return 0      if @c1=3 and @c2%5<>0
             return 0      return 1
    end
    gocreate table table1
    (
      c1 int,
      c2 int,
      constraint df_c1_c2 check(dbo.f_check(c1,c2)>0)
    )