在NULLS的存在下,确保四列的最佳方式不是同一对 [英] Best way to insure four columns are notthe same pair wise in thepresence of NULLS

查看:39
本文介绍了在NULLS的存在下,确保四列的最佳方式不是同一对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑


创建表格t(

..

..

..

a整数,

b varchar(25),

partofa整数,

partofb varchar(25)。

..

..

..


我想编写一个强制执行的检查约束(a = partofa和

b = partofb)每当partofa和partofb都不为空时。我来的是什么
up是一个丑陋的条款,有很多''是null' 's'和''不是空的'。

当然有一个优雅的解决方案。

解决方案

您好。


如果您在DB2 for LUW:

---

not



(a = partofa或coalesce(a,partofa)为null)

和(b = partofb或coalesce(b,partofb)为空)



---

如果你在iSeries:

---

not not (a与partofa没有区别,b与partofb没有区别)

---


此致,

马克B.


考虑


创建表格t(







a整数,

b varchar(25),

partofa整数,

partofb varchar(25)。








我想编写一个强制执行的检查约束(a = partofa和

b = partofb)每当partofa和partofb都不为空。我来的是什么
起来是一个丑陋的条款,有很多''是空的'和''不是空的'。

当然有优雅解决方案。




Bob Stearns写道:


考虑


创建表格t(







a整数,

b varchar(25),

partofa整数,

partofb varchar(25)。








我想编写一个强制执行的检查约束(a = partofa和

b = partofb)每当partofa和partofb都不为空时。我来的是什么?b $ b up是一个丑陋的条款,有很多''是null'和''不是null'的。

肯定有一个优雅的解决方案。



CHECK((partofa IS NULL和partofb IS NULL)或(COALESCE(partofa, a ||

''a'')< a OR COALESCE(partofb,b ||'''')< b))


B.


4.****@mail。 ru 写道:

你好。


如果你在DB2 for LUW:

---





(a = partofa或coalesce(a,partofa)为空)

和(b = partofb或coalesce( b,partofb)为null)



---

如果你在iSeries:

- -

not(a与partofa不同,b与partofb不同)

---


真诚,

Mark B.


>>考虑

创建表t(



一个整数,
b varchar(25),
partofa整数,
partofb varchar(25)。




我想写一个检查约束,当partofa和partofb不是时,它会强制执行(a = partofa和
b = partofb)都是空的。我带来的是一个丑陋的条款,其中有很多''是空的'而且''不是空的'。
肯定有一个优雅的解决方案。




谢谢。


非常聪明。


我应该声明我使用的是DB2 for LUW v8.1.9。拥有DISTINCT

谓词会非常好。你知道它是否在DB2 v9中吗?


Consider

create table t(
..
..
..
a integer,
b varchar(25),
partofa integer,
partofb varchar(25).
..
..
..

I want to write a check constraint which enforces not(a=partofa and
b=partofb) whenever partofa and partofb are not both null. What I came
up with was an ugly clause with lots of ''is null''s and ''is not null''s.
Surely there is an elegant solution for this.

解决方案

Hello.

If you are at DB2 for LUW:
---
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
---
If you are at iSeries:
---
not (a is not distinct from partofa and b is not distinct from partofb)
---

Sincerely,
Mark B.

Consider

create table t(
.
.
.
a integer,
b varchar(25),
partofa integer,
partofb varchar(25).
.
.
.

I want to write a check constraint which enforces not(a=partofa and
b=partofb) whenever partofa and partofb are not both null. What I came
up with was an ugly clause with lots of ''is null''s and ''is not null''s.
Surely there is an elegant solution for this.



Bob Stearns wrote:

Consider

create table t(
.
.
.
a integer,
b varchar(25),
partofa integer,
partofb varchar(25).
.
.
.

I want to write a check constraint which enforces not(a=partofa and
b=partofb) whenever partofa and partofb are not both null. What I came
up with was an ugly clause with lots of ''is null''s and ''is not null''s.
Surely there is an elegant solution for this.

CHECK((partofa IS NULL AND partofb IS NULL) OR (COALESCE(partofa, a ||
''a'') <a OR COALESCE(partofb, b || ''a'') <b))

B.


4.****@mail.ru wrote:

Hello.

If you are at DB2 for LUW:
---
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
---
If you are at iSeries:
---
not (a is not distinct from partofa and b is not distinct from partofb)
---

Sincerely,
Mark B.

>>Consider

create table t(
.
.
.
a integer,
b varchar(25),
partofa integer,
partofb varchar(25).
.
.
.

I want to write a check constraint which enforces not(a=partofa and
b=partofb) whenever partofa and partofb are not both null. What I came
up with was an ugly clause with lots of ''is null''s and ''is not null''s.
Surely there is an elegant solution for this.


Thank you.

That is very clever.

I should have stated I was using DB2 for LUW v8.1.9. Having the DISTINCT
predicate would be very nice. Do you know if it is in DB2 v9?


这篇关于在NULLS的存在下,确保四列的最佳方式不是同一对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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