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
--表約束 alter table t add constraint con_T_ID check(ID not like '%''%')
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的值
--比如。要求年龄只在15~40岁之间而大于或小于的不能插入表中 ALTER TABLE 表名 ADD CONSTRAINT CK_STUAGE CHECK(列名 BETWEEN 15 AND 10)
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
alter table t add constraint con_T_ID check(ID not like '%''%')
--加约束
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的值
--比如。要求年龄只在15~40岁之间而大于或小于的不能插入表中
ALTER TABLE 表名
ADD CONSTRAINT CK_STUAGE CHECK(列名 BETWEEN 15 AND 10)