为已经存在的列添加约束check?我现在就查到创建列时添加check,但是我的列已经存在,怎么办呢? 

解决方案 »

  1.   

    ALTER TABLE tb_name
      ADD CONSTRAINT constraint_name CHECK(col>10);
      

  2.   

    ALTER TABLE doc_exd WITH NOCHECK 
    ADD CONSTRAINT exd_check CHECK (column_a > 1) ;
      

  3.   

    create table ta(id int)
    go
    insert ta select 1;
    select * from taalter table ta with nocheck add constraint add_check check(id > 10);insert ta select 1
    select * from tadrop table ta
    /*(1 行受影响)
    id
    -----------
    1(1 行受影响)消息 547,级别 16,状态 0,第 6 行
    INSERT 语句与 CHECK 约束"add_check"冲突。该冲突发生于数据库"CSDN_TEST",表"dbo.ta", column 'id'。
    语句已终止。
    id
    -----------
    1(1 行受影响)*/