create table user( userName nvarchar 50, pwd nvarchar 10)
如果想限制用户名长度在5-15个字符之间,密码pwd在6-8字符之间该如何改写这条语句啊?
有人说要用check,也有人说可以用trigger,
但关键问题是我不知具体怎么写,以及用什么来得到字段长度
希望哪位老大能帮帮忙,按照我上面的情况给出一个具体的例子。
多谢了!
8 个解决方案
#1
--check约束
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
#2
个人认为,用check约束比用触发器好!
#3
--同意楼上的,用check约束,没必要用trigger
#4
感觉还是用check方便些,
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
其实这种情况基本上都是在客户端前台来限制的。。。比后台判断更好一些。。。
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
其实这种情况基本上都是在客户端前台来限制的。。。比后台判断更好一些。。。
#5
create table [user]
(
userName nvarchar(50) check(len(userName) between 5 and 15),
pwd nvarchar(10) check(len(pwd) between 6 and 8)
)
-- or
create table [user]
(
userName nvarchar(50),
pwd nvarchar(10) check(len(pwd) between 6 and 8),
constraint ch_user check((len(userName) between 5 and 15) and (len(pwd) between 6 and 8))
)
(
userName nvarchar(50) check(len(userName) between 5 and 15),
pwd nvarchar(10) check(len(pwd) between 6 and 8)
)
-- or
create table [user]
(
userName nvarchar(50),
pwd nvarchar(10) check(len(pwd) between 6 and 8),
constraint ch_user check((len(userName) between 5 and 15) and (len(pwd) between 6 and 8))
)
#6
--笔误更正
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
#7
--测试环境
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
--测试数据
insert [user] values('shuiniu','abc') --失败,密码长度不够
insert [user] values('shui','abcdefg') --失败,用户名长度长度不够
insert [user] values('shuiniu','shuiniu') --成功
select * from [user]
/*
userName pwd
-------------------------------------------------- ----------
shuiniu shuiniu
(所影响的行数为 1 行)
*/
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
--测试数据
insert [user] values('shuiniu','abc') --失败,密码长度不够
insert [user] values('shui','abcdefg') --失败,用户名长度长度不够
insert [user] values('shuiniu','shuiniu') --成功
select * from [user]
/*
userName pwd
-------------------------------------------------- ----------
shuiniu shuiniu
(所影响的行数为 1 行)
*/
#8
多谢!
#1
--check约束
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
#2
个人认为,用check约束比用触发器好!
#3
--同意楼上的,用check约束,没必要用trigger
#4
感觉还是用check方便些,
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
其实这种情况基本上都是在客户端前台来限制的。。。比后台判断更好一些。。。
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd between 6 and 8)))
其实这种情况基本上都是在客户端前台来限制的。。。比后台判断更好一些。。。
#5
create table [user]
(
userName nvarchar(50) check(len(userName) between 5 and 15),
pwd nvarchar(10) check(len(pwd) between 6 and 8)
)
-- or
create table [user]
(
userName nvarchar(50),
pwd nvarchar(10) check(len(pwd) between 6 and 8),
constraint ch_user check((len(userName) between 5 and 15) and (len(pwd) between 6 and 8))
)
(
userName nvarchar(50) check(len(userName) between 5 and 15),
pwd nvarchar(10) check(len(pwd) between 6 and 8)
)
-- or
create table [user]
(
userName nvarchar(50),
pwd nvarchar(10) check(len(pwd) between 6 and 8),
constraint ch_user check((len(userName) between 5 and 15) and (len(pwd) between 6 and 8))
)
#6
--笔误更正
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
#7
--测试环境
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
--测试数据
insert [user] values('shuiniu','abc') --失败,密码长度不够
insert [user] values('shui','abcdefg') --失败,用户名长度长度不够
insert [user] values('shuiniu','shuiniu') --成功
select * from [user]
/*
userName pwd
-------------------------------------------------- ----------
shuiniu shuiniu
(所影响的行数为 1 行)
*/
create table [user]( userName nvarchar(50) check(len(username) between 5 and 15), pwd nvarchar(10) check(len(pwd) between 6 and 8))
--测试数据
insert [user] values('shuiniu','abc') --失败,密码长度不够
insert [user] values('shui','abcdefg') --失败,用户名长度长度不够
insert [user] values('shuiniu','shuiniu') --成功
select * from [user]
/*
userName pwd
-------------------------------------------------- ----------
shuiniu shuiniu
(所影响的行数为 1 行)
*/
#8
多谢!