CREATE TABLE [dbo].[state_record](
[row_id] [int] NOT NULL,
[state_id] [smallint] NOT NULL,
[user_login] [varchar](24) NOT NULL,
[time_entered] [datetime] NOT NULL,
[comment] [varchar](256) NOT NULL,
[end_time] [datetime] NOT NULL
)
ALTER TABLE [dbo].[state_record] WITH CHECK ADD CONSTRAINT state_end_time_constraint] CHECK (([time_entered]<=[end_time]))
ALTER TABLE [dbo].[alert_state_record] ADD CONSTRAINT [DF__alert_sta__time___3E723F9C] DEFAULT (getdate()) FOR [time_entered]
INSERT INTO alert_state_record(row_id, state_id, user_login, comment, end_time)
values(1,1,'max','resolving', getdate())
插入此表有时会产生错误“INSERT 语句与 CHECK 约束“state_end_time_constraint”冲突。这是因为 time_entered 的值是在 end_time 之后计算的。
INSERT INTO alert_state_record(row_id, state_id, user_login, comment, time_entered, end_time)
values(1,1,'max','resolving', getdate(), getdate()) however works