请哪位帮我写写这个检查约束。谢了。我自己试着写了很久都不是很好。。类型,char(6)可以为null
可以为 6个空格
可以为[0-1][0-9][0-9][:][0-5][0-9]谢了。

解决方案 »

  1.   

    create table tb(col char(6))
    go
    alter table tb
      add constraint chk_tb check(col='      ' or col like '[0-1][0-9][0-9][:][0-5][0-9]')
    go
      

  2.   

    if object_id('[tb]') is not null drop table [tb] 
     go 
    create table [tb]([col] char(6),check(col like '[0-1][0-9][0-9][:][0-5][0-9]' or col is null or col like '[ ][ ][ ][ ][ ][ ]'))/*
    可以为null 
    可以为 6个空格 
    可以为[0-1][0-9][0-9][:][0-5][0-9] */insert tb select '123456'
    /*
    消息 547,级别 16,状态 0,第 1 行
    INSERT 语句与 CHECK 约束"CK__tb__col__110B679F"冲突。该冲突发生于数据库"test1",表"dbo.tb", column 'col'。
    语句已终止。
    */
    insert tb select '      '
    /*
    (1 行受影响)
    */
    insert tb select NULL
    /*
    (1 行受影响)
    */
      

  3.   

    ALTER table LI add constraint c_LI CHECK(COL1 LIKE '[0-1][0-9][0-9][:][0-5][0-9]')