给数据库添加检查约束 不允许有单引号 这个约束怎么写啊 刚学习这个 

解决方案 »

  1.   


    use tempdb
    gocreate table test(col varchar(20))
    goalter table test
    add constraint ck_col check(charindex('''',col)<=0)
    goinsert test
    select 'abc'''
    go
    drop table test
    go
      

  2.   

    --表約束
    alter table t add constraint con_T_ID check(ID not like '%''%')
      

  3.   

    create table tb(id int )
    --加约束
    alter table tb add constraint CK_tb check (id<100)insert into tb(id) values(200)
    ---结果错误
    INSERT statement conflicted with COLUMN CHECK constraint 'CK_tb'. The conflict occurred in database 'guide_OA', table 'tb', column 'id'.
    The statement has been terminated.只能存储id<100的值
      

  4.   


    --比如。要求年龄只在15~40岁之间而大于或小于的不能插入表中 
    ALTER TABLE 表名 
    ADD CONSTRAINT CK_STUAGE CHECK(列名 BETWEEN 15 AND 10)