Oracle SQL - 可以使用CASE在检查约束中确定数据属性吗? [英] Oracle SQL - can CASE be used in a check constraint to determine data attributes?

查看:130
本文介绍了Oracle SQL - 可以使用CASE在检查约束中确定数据属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Oracle 10g,我想对一个表应用约束,其中为一列输入的值决定另一列是NULL还是IS NOT NULL。 Column1只能包含1或0; Column2是VARCHAR2(255)。

I'm using Oracle 10g and I want to apply a constraint to a table where the value entered for one column determines whether another column IS NULL or IS NOT NULL. Column1 can only contain 1 or 0; Column2 is VARCHAR2(255).

我知道以下工作:

CONSTRAINT ck_1 CHECK ((col1=1 AND col2 IS NOT NULL) OR (col1=0 AND col2 IS NULL));

但是,我想知道是否可以使用CASE来执行这个约束并设置属性NOT在col2上为NULL,或者CASE只能用于定义值吗?即可以这样工作:

However, I was wondering if it is possible to use CASE to perform this constraint and set the attribute NOT NULL on col2, or can CASE only be used to define values? i.e. could something like this work:

CONSTRAINT ck_1 CHECK (CASE WHEN col1=1 THEN col2 IS NOT NULL ELSE col2 IS NULL END);


推荐答案

由于CASE表达式必须返回一个值,是布尔值,你必须比较结果与某些,例如:

Since CASE expressions must return a value, and check constraints are boolean, you'll have to compare the result with something, e.g.:

CONSTRAINT ck_1 CHECK (CASE WHEN col2 IS NOT NULL THEN 1 ELSE 0 END = col1);

这篇关于Oracle SQL - 可以使用CASE在检查约束中确定数据属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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