帮忙下这个sql语句为什么不能创建表

fermin123567 2014-01-01 08:31:36
create table test
(
id int not null primary key identity(1,1),
xy int check( xy>[min] and xy<[max]),
[min] int default(0),
[max] int default(1000)
)
...全文
173 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fermin123567 2014-01-01
  • 打赏
  • 举报
回复
thanks
LongRui888 2014-01-01
  • 打赏
  • 举报
回复
引用 5 楼 u012766794 的回复:
这样是可以的,但是这样不是我的目的呀,我想让xy的取值介于min和max之间
这个得建立触发器:

create table test
(
id int not null primary key identity(1,1),
xy int ,
[min] int default(0),
[max] int default(1000)
)
go

create trigger dbo.trigger_test_insert
on test
for insert
as

if exists(select * from test where  not(xy>[min] and xy<[max]))
   rollback

go

--1.满足,不会报错
insert into test
select 10,1,11


--2.不满足条件,所以报错,插入不了
insert into test
select 10,1,10
/*
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/
fermin123567 2014-01-01
  • 打赏
  • 举报
回复
这样是可以的,但是这样不是我的目的呀,我想让xy的取值介于min和max之间
LongRui888 2014-01-01
  • 打赏
  • 举报
回复
引用 2 楼 u012766794 的回复:
我不用关键字,也创建不了这个表
这样呢,应该可以的把:
create table test
(
id int not null primary key identity(1,1),
xy int ,
[min] int default(0),
[max] int default(1000)
)
tenhilltree 2014-01-01
  • 打赏
  • 举报
回复
fermin123567 2014-01-01
  • 打赏
  • 举报
回复
我不用关键字,也创建不了这个表
LongRui888 2014-01-01
  • 打赏
  • 举报
回复
create table test ( id int not null primary key identity(1,1), xy int check( xy>[min] and xy<[max]), [min] int default(0), [max] int default(1000) ) 这个红色的部分是不对的,因为在sql server中check约束中,只能引用其他列的字段,比如max 和min 都是不行的。 建议这个用触发器来实现

34,593

社区成员

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

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