时间的约束问题

技术科 2008-03-12 11:10:55
创建如下表
create table bbsTopic
(
TID int not null primary key identity(1,1),
TsID int not null constraint FK_TsID foreign key (TsID) references bbsSection(SID),
TuID int not null constraint FK_TuID foreign key (TuID) references bbsUsers(UID),
TreplyCount int default(0),
Tface int,
Ttopic varchar(20) not null,
Tcontents varchar(30) not null constraint CK_Tcontents check (len(Tcontents)>6),
Ttime datetime default(getDate()),
TclickCount int default(0),
Tstate int not null default(1),
TlastReply datetime constraint CK_TlastReply check (TlastReply>Ttime)
)

红色部分要求为:在Tstate中输入的时间必须大于Ttime中的时间,按上面的写法,报错如下:
服务器: 消息 8141,级别 16,状态 1,行 2
列 CHECK 约束(属于列 'TlastReply')引用了另一列,表 'bbsTopic'。

请教红色Tstate列的约束如何写?

...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzy_love_sly 2008-03-12
  • 打赏
  • 举报
回复
--drop table index_tb
create table index_tb(id int constraint ck_id default 0,name varchar(20),xb varchar(2))
--查看表
exec sp_helpindex orders--索引
exec sp_helpconstraint index_tb --约束
exec sp_fkeys 'dbo.Person'--外键
--键不唯一,非聚集索引
create index id_index on index_tb(id)
--删除索引
drop index index_tb.id_index
--键聚集索引
create clustered index id_index on index_tb(id)
--删除索引
drop index index_tb.id_index
--创建check约束
alter table index_tb add constraint ck_index CHECK (name like '[0-9][0-9][0-9][0-9][0-9]')
alter table index_tb add constraint ck2_index check(xb in ('MM','GG'))
--删除check约束
alter table index_tb drop constraint ck2_index
--禁止表约束
alter table index_tb nocheck constraint ck_index
--恢复表约束
alter table index_tb check constraint ck_index
--禁止和恢复所有check-all关键字
select 'alter table '+name+' nocheck or check constraint all' from sysobjects where type='U'
--添加新默认值和约束
ALTER TABLE index_tb ADD CONSTRAINT de_name DEFAULT ('11111') FOR name
--删除表约束
alter table index_tb drop constraint de_name
-- 字段 UNIQUE NOT NULL = PRIMARY KEY 差不多


insert into index_tb(name,xb) select '12345','mm'

select * from index_tb

delete index_tb


wzy_love_sly 2008-03-12
  • 打赏
  • 举报
回复
create table tb(dtstart datetime,dtend datetime)
alter table tb add constraint tb_index check(dtstart>dtend)
insert into tb select getdate(),getdate()+1


消息 547,级别 16,状态 0,第 1 行
INSERT 语句与 CHECK 约束"tb_index"冲突。该冲突发生于数据库"my",表"dbo.tb"。
语句已终止。
lhsxsh 2008-03-12
  • 打赏
  • 举报
回复
你就写下
TlastReply =Ttime+1
试试看
lhsxsh 2008-03-12
  • 打赏
  • 举报
回复
ss

34,592

社区成员

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

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