473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Best way to insure four columns are notthe same pair wise in thepresence of NULLS

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.
Aug 8 '06 #1
12 1416
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.
Aug 8 '06 #2

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.

Aug 8 '06 #3
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?
Aug 8 '06 #4
Brian Tkatch wrote:
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.
This will work with a minor modification: make "a||'a'" into "a+1" since
"||" is not defined on integers. Thank you.
Aug 8 '06 #5

Bob Stearns wrote:
Brian Tkatch wrote:
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.
This will work with a minor modification: make "a||'a'" into "a+1" since
"||" is not defined on integers. Thank you.
Heh, i should have looked at your TABLE definition! Shame on me. :)

Glad i could be of service.

B.

Aug 9 '06 #6
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.
Is following thinking right?

If (a is null and partofa is null and b = '1' and partofb = '1'),
Condition "partofa and partofb are not both null" is satisfied.
1)
not(a=partofa and b=partofb) =: not(Unknown and True) =: not(Unknown)
=: Unknown
2)
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
=: not( (Unknown or True) and (True or False) )
=: not( True and True)
=: False
Condition 1) satisfy CHECK condition. Condition 2) don't satisfy CHECK
condition.

Aug 11 '06 #7

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.
Is following thinking right?

If (a is null and partofa is null and b = '1' and partofb = '1'),
Condition "partofa and partofb are not both null" is satisfied.
1)
not(a=partofa and b=partofb) =: not(Unknown and True) =: not(Unknown)
=: Unknown
2)
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
=: not( (Unknown or True) and (True or False) )
=: not( True and True)
=: False
Condition 1) satisfy CHECK constraint. Condition 2) don't satisfy CHECK

constraint.

Aug 11 '06 #8
Bob, did I misunderstand?
whenever partofa and partofb are not both null
Does it mean "partofa IS NOT NULL AND partofb IS NOT NULL"

Aug 12 '06 #9
Tonkuma wrote:
Bob, did I misunderstand?
whenever partofa and partofb are not both null
Does it mean "partofa IS NOT NULL AND partofb IS NOT NULL"
And do you want if both of a and partofa are null, assume they are
equal?

Aug 12 '06 #10
Tonkuma wrote:
Tonkuma wrote:
>>Bob, did I misunderstand?
>>>whenever partofa and partofb are not both null

Does it mean "partofa IS NOT NULL AND partofb IS NOT NULL"


And do you want if both of a and partofa are null, assume they are
equal?
Yes and Yes
Aug 12 '06 #11

Bob Stearns wrote:
Tonkuma wrote:
Tonkuma wrote:
>Bob, did I misunderstand?

whenever partofa and partofb are not both null

Does it mean "partofa IS NOT NULL AND partofb IS NOT NULL"

And do you want if both of a and partofa are null, assume they are
equal?
Yes and Yes
So, if you would use following CHECK constraint,
you can't insert data like (a is null and partofa is null and b = '1'
and partofb = '1').
Because CHECK constraint would return False.
CHECK(
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
)

Is it your requirement?
(This data doesn't satisfy condition "partofa IS NOT NULL AND partofb
IS NOT NULL")

Aug 12 '06 #12
Tonkuma wrote:
Bob Stearns wrote:
>>Tonkuma wrote:
>>>Tonkuma wrote:
Bob, did I misunderstand?
>whenever partofa and partofb are not both null

Does it mean "partofa IS NOT NULL AND partofb IS NOT NULL"
And do you want if both of a and partofa are null, assume they are
equal?

Yes and Yes

So, if you would use following CHECK constraint,
you can't insert data like (a is null and partofa is null and b = '1'
and partofb = '1').
Because CHECK constraint would return False.
CHECK(
not
(
(a=partofa or coalesce(a, partofa) is null)
and (b=partofb or coalesce(b, partofb) is null)
)
)

Is it your requirement?
(This data doesn't satisfy condition "partofa IS NOT NULL AND partofb
IS NOT NULL")
Exactly. Consider (a, b) as primary key (it isn't, but that discussion
is for another day) and (partofa, partofb) as a recursive foreign key.
The requirement is that if (partofa, partofb) is not (null, null) then
it must be different from (a, b); i. e. not a reference to itself.
Aug 12 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
0
by: qazmlp | last post by:
I have got a sorted vector of pair<string, string> which contains the following entries: first string second string Pair-1: 121721234567890 str1One Pair-2: 121721234567890 str1 Pair-3:...
9
by: Bob Stearns | last post by:
What is considered the best way to constrain an attribute to be unique if it IS NOT NULL? Should I look at a trigger ON INSERT OR UPDATE, or is there a simpler way? The following attempt does not...
1
by: melanie | last post by:
Hi, I open a recordset with a SQL query as its source. Then, i set the listbox.recordset = to the recordset i just opened. But, the fields appear in the listbox columns in a different order...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
7
by: teddysnips | last post by:
Table DDL below: The tables I have contain Timesheet information. Each row in the tblTSCollected table contains an entry for an employee into the timesheet system, specifically by scanning the...
5
by: Ronald S. Cook | last post by:
We have a .NET Win app that runs at 12 different cattle feeding lots. Each lot runs an isolated instance of our app using its own SQL Server instance. For overall settings specific to a feedlot,...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
8
by: DaFrizzler | last post by:
Hi, I have received the following email from a colleague, and am quite frankly baffled by the idea. I am just wondering if anyone has any advice or suggestions about this???? === BEGIN MAIL...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.