age int 4 年龄 18-50岁
这个chick约束改怎么写啊

解决方案 »

  1.   

    check(age>=18 and age<=50)
      

  2.   


    alter table T add check (年龄>=18 and 年龄<=50)
      

  3.   

    if object_id('tempdb..#')is not null drop table #
    go
    create table #(Id int identity(1,1), age int check(age between 18 and 50))
    go
    insert # select 18
    insert # select 60
    /*
    (所影响的行数为 1 行)服务器: 消息 547,级别 16,状态 1,行 2
    INSERT statement conflicted with COLUMN CHECK constraint 'CK__#__age__060DEAE8'. The conflict occurred in database 'tempdb', table '#___________________________________________________________________________________________________________________000000000011', column 'age'.
    The statement has been terminated.*/
      

  4.   

    If Object_id('Employee','U') Is Not null
    Drop Table Employee
    Go
    Create Table Employee
    (
    ID int Identity(1,1) Not null,
    Name nvarchar(50),
    Age int,
    Constraint PK_Employee_ID Primary key(ID Asc),
    Constraint C_Employee_Age Check(Age Between 18 And 80 ) 
    )
    Insert Into Employee Select N'张三',34 --ok
    Insert Into Employee Select N'张三',16 --Error
    /*
    消息 547,级别 16,状态 0,第 10 行
    INSERT 语句与 CHECK 约束"C_Employee_Age"冲突。该冲突发生于数据库"Test",表"dbo.Employee", column 'Age'。
    语句已终止。*/
      

  5.   

    alter table T add check (年龄>=18 and 年龄<=50)这个改的或者说 
    check(age>=18 and age <=50) 这个是写
      

  6.   


    alter table add constraint chech_age check(age>=15 and age<=50)
      

  7.   


    alter table tb add constraint chech_age check(age>=15 and age<=50)/*对现在有的数据不验证*/
    alter table tb with nocheck add constraint chech_age check(age>=15 and age<=50)/*对现有的数据进行验证*/