当前位置: 代码迷 >> Sql Server >> 此约束咋筑啊大哥
  详细解决方案

此约束咋筑啊大哥

热度:72   发布时间:2016-04-24 23:19:00.0
此约束咋建啊,大哥
题目是这样的:为用户名列建立约束
要求如下:
1.长度必须为6
2.第1位和第2位必须为数字
3.3~6位必须为英文字母

我知道要建立check约束
因此check(Len(userName)=6)
可是后面不知道了,各位大神帮帮忙啊

------解决方案--------------------
ALTER table tb add constraint ysm check(len(txt)=6 and txt like '[0-9][0-9][a-z][a-z][a-z][a-z]')


------解决方案--------------------

if OBJECT_ID('tab') is not null
drop table tab
go
create table tab(name varchar(10),
 constraint ck_tab   check(
name like '[0-9][0-9][a-z][a-z][a-z][a-z]' or 
name like '[0-9][0-9][A-Z][A-Z][A-Z][A-Z]'))


------解决方案--------------------
默认一般不区分大小写
check(userName like '[0-9][0-9][a-z][a-z][a-z][a-z]')
也可以
check(userName like '[0-9][0-9][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]')
------解决方案--------------------
create table tabsA(
 username varchar(6),
 constaint check(len(username)=6 and username like '[0-1][0-1][0-1][a-z][a-z][a-z]')
);
  相关解决方案