1.在存插入存储过程中进行校验。
这样,使用存储过程插入数据,这里最常用的方法,可以最大限度地加入限制,报错和纠错功能以及完成其它表的关联操作。
使用起来也很方便:
EXEC TABLENAME_PROC '123456'
--以下是存储过程脚本
create tablename_proc
@field1 varchar(6)
as
if @field1 not like [0-9][0-9][0-9][0-9][0-9][0-9]
begin
reserror('必须是六位数字',16,1
return
end
insert into tablename(field1) values(@field1)
go
2.建表时加入约束:
create tablename(field1 CHECK (FIELD1 LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]')
3.建表后给已存在字段添加约束
alter table TABLENAME
with nocheck add constraint TABLENAME_check
check(FIELD1 LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]')
温馨提示:答案为网友推荐,仅供参考