建表时是否可以同时在一列上建两个约束

yo_258 2008-06-16 04:55:11
如果可以,(sex char(2) 默认值为男,只能输入男或女
...全文
281 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy-W 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zbc1009 的回复:]
引用 8 楼 zbc1009 的回复:
SQL codecreate table t(sex nchar(4) default('男') check(sex not in (N'男', N'女')))

insert t select '男'
--error
insert t select '女'
--error
insert t select '不男不女'
--success

select * from t

drop table t


哈哈

[/Quote]
zbc1009 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zbc1009 的回复:]
SQL codecreate table t(sex nchar(4) default('男') check(sex not in (N'男', N'女')))

insert t select '男'
--error
insert t select '女'
--error
insert t select '不男不女'
--success

select * from t

drop table t
[/Quote]
zbc1009 2008-06-16
  • 打赏
  • 举报
回复
create table t(sex nchar(4) default('男') check(sex not in (N'男', N'女')))

insert t select '男'
--error
insert t select '男'
--error
insert t select '不男不女'
--success

select * from t

drop table t
nnoovvee 2008-06-16
  • 打赏
  • 举报
回复
jihao
cacar2008 2008-06-16
  • 打赏
  • 举报
回复
create table t(sex nchar(1) default('男') check(sex in (N'男', N'女')))

insert t select '男'

drop table t
utpcb 2008-06-16
  • 打赏
  • 举报
回复
create table t(sex nchar(1) default('男') check(sex in (N'男', N'女')))

insert t select '男'

select * from t

drop table t
dobear_0922 2008-06-16
  • 打赏
  • 举报
回复
create table t(sex nchar(1) default('男') check(sex in (N'男', N'女')))

insert t select '男'

select * from t

drop table t
arrow_gx 2008-06-16
  • 打赏
  • 举报
回复
用 bit 类型 1 代表男, 0代表女,默认是 1 ,什么约束都不用建立,反正 bit 类型,就是 0 和1 两个可能
liangCK 2008-06-16
  • 打赏
  • 举报
回复
create table #t(sex nchar(2),col2 int)

alter table #t
add constraint check_cons check(sex in(N'男',N'女'))
go
alter table #t
add constraint df_cons default N'男' for sex
go

insert into #t(sex,col2) values(N'男',12)

insert into #t(sex) values(N'神')

insert into #t(col2) values(12)

select * from #t

drop table #t
Herb2 2008-06-16
  • 打赏
  • 举报
回复
当然可以

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧