两列之间的列之一应为 NOT NULL.如何在模式中强制执行它? [英] One of the column between two columns should be NOT NULL. How to enforce it in schema?

查看:25
本文介绍了两列之间的列之一应为 NOT NULL.如何在模式中强制执行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下 sehma 的表格

I have a table with following scehma

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

现在插入值时,FirstIdentifier 和 SecondIdentifier 之间的列之一应该是非空.有没有通过模式强制执行它?

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?

推荐答案

这可以通过使用 CHECK 约束:

This is possible with using a CHECK constraint:

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

虽然 CHECK 约束是表的一部分(因此是模式"?),但它们可能不符合所需的定义.上述 CHECK 并不相互排斥,但可以更改为这样.

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.

快乐编码.

这篇关于两列之间的列之一应为 NOT NULL.如何在模式中强制执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆