【发布时间】:2013-12-02 11:56:49
【问题描述】:
我想限制一列只有在另一列有值时才能有值。
示例:(这不起作用)
create table testConstraint (
col1 int not null identity(1, 1) primary key,
col2 int,
col3 int check (col2 is not null),
col4 int)
这是不可能的,因为他无法引用另一列。 错误:
列 'col3' 的列 CHECK 约束引用另一列, 表'testConstraint'。
另一个尝试是:(也不起作用)
create table testConstraint (
col1 int not null identity(1, 1) primary key,
col2 int,
col3 int,
col4 int)
GO
alter table testConstraint add constraint ck_columnNotNull check (case when col2 is null then col3 is null end)
GO
任何人都知道如何通过约束实现这一点?
【问题讨论】:
标签: sql sql-server constraints