我必须跟踪 SQL Server 2008 R2 中的数据。SQLFiddle
架构:
创建表 [dbo].[ICFilters](
[ICFilterID] [int] IDENTITY(1,1) 非空,
[ParentID] [int] NOT NULL DEFAULT 0,
[FilterDesc] [varchar](50) NOT NULL,
[Active] [tinyint] NOT NULL DEFAULT 1,
约束 [PK_ICFilters] 主键聚集
( [ICFilterID] ASC ) 与
PAD_INDEX = 关闭,
STATISTICS_NORECOMPUTE = 关闭,
IGNORE_DUP_KEY = 关闭,
ALLOW_ROW_LOCKS = 开,
ALLOW_PAGE_LOCKS = 开
) 在 [主要]
) 在 [主要]
INSERT INTO [dbo].[ICFilters] (ParentID,FilterDesc,Active)
价值观
(0,'产品类型',1),
(1,'ProdSubType_1',1),
(1,'ProdSubType_2',1),
(1,'ProdSubType_3',1),
(1,'ProdSubType_4',1),
(2,'PST_1.1',1),
(2,'PST_1.2',1),
(2,'PST_1.3',1),
(2,'PST_1.4',1),
(2,'PST_1.5',1),
(2,'PST_1.6',1),
(2,'PST_1.7',0),
(3,'PST_2.1',1),
(3,'PST_2.2',0),
(3,'PST_2.3',1),
(3,'PST_2.4',1), … 是否可以根据行中的其他值来限制列中允许的值?
例如,我的表:
ID Test_mode Active
-- --------- ------
1 1 Null
2 0 1
3 1 0
Run Code Online (Sandbox Code Playgroud)
有没有办法要么改变的值Test_mode来0如果1插入Active
或者
如果Test_mode是 1 不允许插入/更新Active
或者
如果Test_mode是 1 并Active尝试插入/更新,则抛出某种错误。
Active只能是 NULL, 1, 0, AND only 1 with Test_modeas 0.
我希望这是有道理的,如果没有让我知道,我会更新问题。