我想通过约束机制实现在某一条件下约束生效。例如:
create table person
(
id int not null,
status int not null
)
当status为0时保证id唯一没有重复,而status为非0时id可不唯一。

解决方案 »

  1.   

    check约束能约束范围,能约束唯一吗?这块不是很懂
      

  2.   

    不行,不能用check约束,它里面不能加子查询.
      

  3.   

    --用触发器吧
    CREATE TRIGGER [TRIGGER_person] ON [dbo].[person] 
    FOR INSERT, UPDATE, DELETE 
    AS
    begin
    if (select top 1 count(a.id) from person a,inserted b 
    where a.status=0  and b.status=0 and a.id=b.id 
    group by a.id order by count(a.id) desc)>1rollback
    end