两列之间的列之一应为NOT NULL。如何在架构中强制执行?

时间:2022-11-16 16:31:43

I have a table with following scehma

我有一张表有以下的气味

CREATE TABLE MyTable  
(  
    ID                INTEGER DEFAULT(1,1),      
    FirstIdentifier   INTEGER NULL,    
    SecondIdentifier  INTEGER NULL,  
  --.... some other fields .....    
)   

Now when inserting a value, one of the column between FirstIdentifier and SecondIdentifier should be NOT NULL. Is there anyway of enforcing it via schema?

现在,当插入值时,FirstIdentifier和SecondIdentifier之间的列之一应为NOT NULL。无论如何通过架构强制执行它?

1 个解决方案

#1


19  

This is possible with using a CHECK constraint:

使用CHECK约束可以实现这一点:

CHECK (FirstIdentifier IS NOT NULL OR SecondIdentifier IS NOT NULL)

While CHECK constraints are part of the table (and hence "schema"?), they may not fit the desired definition. The above CHECK is not mutually exclusive, but it could be altered to such.

虽然CHECK约束是表的一部分(因此是“模式”?),但它们可能不符合所需的定义。上面的CHECK不是互斥的,但可以改为这样。

Happy coding.

快乐的编码。

#1


19  

This is possible with using a CHECK constraint:

使用CHECK约束可以实现这一点:

CHECK (FirstIdentifier IS NOT NULL OR SecondIdentifier IS NOT NULL)

While CHECK constraints are part of the table (and hence "schema"?), they may not fit the desired definition. The above CHECK is not mutually exclusive, but it could be altered to such.

虽然CHECK约束是表的一部分(因此是“模式”?),但它们可能不符合所需的定义。上面的CHECK不是互斥的,但可以改为这样。

Happy coding.

快乐的编码。