473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unique constraint and NULL values

I want to enforce such a constraint on a column that would ensure that the
values be all unique, but this wouldn't apply to NULL values. (I.e. there
may be more than one NULL value in the column.)
How can I achieve this?
I suppose I would get the most-hated "table/view is changing,
trigger/function may not see it" error if I tried to write a trigger that
checks the uniqueness of non-null values upon insert/update.
Jul 19 '05 #1
26 45394

"Agoston Bejo" <gu***@freemail .hu> wrote in message
news:cl******** **@news.caesar. elte.hu...
| I want to enforce such a constraint on a column that would ensure that the
| values be all unique, but this wouldn't apply to NULL values. (I.e. there
| may be more than one NULL value in the column.)
| How can I achieve this?
| I suppose I would get the most-hated "table/view is changing,
| trigger/function may not see it" error if I tried to write a trigger that
| checks the uniqueness of non-null values upon insert/update.
|
|

did you try a standard UNIQUE constraint on the column?

unlike SQL-Server (unless they've changed it since I last worked on it),
Oracle processes null values properly in this scenario (i.e., one NULL value
is never consider equal to another NULL value)

++ mcs
Jul 19 '05 #2
"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote:

"Agoston Bejo" <gu***@freemail .hu> wrote in message
news:cl******* ***@news.caesar .elte.hu...
| I want to enforce such a constraint on a column that would ensure that the
| values be all unique, but this wouldn't apply to NULL values. (I.e. there
| may be more than one NULL value in the column.)
| How can I achieve this?
| I suppose I would get the most-hated "table/view is changing,
| trigger/function may not see it" error if I tried to write a trigger that
| checks the uniqueness of non-null values upon insert/update.
|
|

did you try a standard UNIQUE constraint on the column?

unlike SQL-Server (unless they've changed it since I last worked on it),
Oracle processes null values properly in this scenario (i.e., one NULL value
is never consider equal to another NULL value)

++ mcs

Yep..A Unique index can enforce a Unique Constraint and can handle NULLs ..A Primary key, on the other hand, cannot have
NULLs..

Jul 19 '05 #3
Am Thu, 21 Oct 2004 09:37:42 -0400 schrieb Mark C. Stock
<mcstockX@Xenqu ery .com>:


did you try a standard UNIQUE constraint on the column?

unlike SQL-Server (unless they've changed it since I last worked on it),
Oracle processes null values properly in this scenario (i.e., one NULL
value
is never consider equal to another NULL value)
Yes, but it's a matter of convention because "NULL <> NULL" evaluates
to null and thus is also not true. So two null values should never
be considered different.
++ mcs


--
Frank Piron,
etfrankatkonadd otn
(leftrotate two)
Jul 19 '05 #4
See the answer below.

"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote in message
news:zs******** ************@co mcast.com...
"Agoston Bejo" <gu***@freemail .hu> wrote in message
news:cl******** **@news.caesar. elte.hu...
| I want to enforce such a constraint on a column that would ensure that the | values be all unique, but this wouldn't apply to NULL values. (I.e. there | may be more than one NULL value in the column.)
| How can I achieve this?
| I suppose I would get the most-hated "table/view is changing,
| trigger/function may not see it" error if I tried to write a trigger that | checks the uniqueness of non-null values upon insert/update.
|
|

did you try a standard UNIQUE constraint on the column?

Yes, I did, and (1, null) and (1, null) couldn't be inserted twice. The
Oracle version I'm currently using (or to be more exact forced to use) is
8.1.7. Maybe in later versions this was corrected, I don't know. Here, when
I tried it, it worked the way I described in my original post.

unlike SQL-Server (unless they've changed it since I last worked on it),
Oracle processes null values properly in this scenario (i.e., one NULL value is never consider equal to another NULL value)

++ mcs

Jul 19 '05 #5

"Agoston Bejo" <gu***@freemail .hu> wrote in message
news:cl******** **@news.caesar. elte.hu...
| See the answer below.
|
| "Mark C. Stock" <mcstockX@Xenqu ery .com> wrote in message
| news:zs******** ************@co mcast.com...
| > "Agoston Bejo" <gu***@freemail .hu> wrote in message
| > news:cl******** **@news.caesar. elte.hu...
| > | I want to enforce such a constraint on a column that would ensure that
| the
| > | values be all unique, but this wouldn't apply to NULL values. (I.e.
| there
| > | may be more than one NULL value in the column.)
| > | How can I achieve this?
| > | I suppose I would get the most-hated "table/view is changing,
| > | trigger/function may not see it" error if I tried to write a trigger
| that
| > | checks the uniqueness of non-null values upon insert/update.
| > |
| > |
| >
| > did you try a standard UNIQUE constraint on the column?
|
|
| Yes, I did, and (1, null) and (1, null) couldn't be inserted twice. The
| Oracle version I'm currently using (or to be more exact forced to use) is
| 8.1.7. Maybe in later versions this was corrected, I don't know. Here,
when
| I tried it, it worked the way I described in my original post.
|
| >
| > unlike SQL-Server (unless they've changed it since I last worked on it),
| > Oracle processes null values properly in this scenario (i.e., one NULL
| value
| > is never consider equal to another NULL value)
| >
| > ++ mcs
| >
| >
|
|

well, that's different than what you posted -- you want to enforce a
constraint on a pair of columns, not on a single column

what i've done in cases like this is create a 'shadow table' (my term, not
oracle's) that contains the two values, plus the primary key, just for rows
where both values are NOT NULL -- i put the 2-column UNIQUE constraint on
the shadow table instead of the original table, and then use a DML trigger
on the original table to maintain the shadow table -- any constraint
violations on the shadow table will propagate (through the trigger) to the
original table

(a variation of this technique also allows DRI across database links,
assuming the links are reliable)

++ mcs
Jul 19 '05 #6

"Frank Piron" <em***@zero.nil > wrote in message
news:op******** ******@news.onl ine.de...
| Am Thu, 21 Oct 2004 09:37:42 -0400 schrieb Mark C. Stock
| <mcstockX@Xenqu ery .com>:
| >
| >
| > did you try a standard UNIQUE constraint on the column?
| >
| > unlike SQL-Server (unless they've changed it since I last worked on it),
| > Oracle processes null values properly in this scenario (i.e., one NULL
| > value
| > is never consider equal to another NULL value)
|
| Yes, but it's a matter of convention because "NULL <> NULL" evaluates
| to null and thus is also not true. So two null values should never
| be considered different.
|
| > ++ mcs
| >
| >
|
| --
| Frank Piron,
| etfrankatkonadd otn
| (leftrotate two)

yes, in theory, the SQL Server approach is more 'pure', but in practice, the
Oracle approach is more practical (i'm starting to sound like yogi berra!)

if i'm creating a UNIQUE constraint, my goal is to disallow values that are
known to be equal to other known values -- so, it's entirely appropriate to
not disallow (sorry for the double-negs) unknown values (triple-negs?) since
there is no known basis for excluding them

++ mcs


Jul 19 '05 #7
"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote:
yes, in theory, the SQL Server approach is more 'pure', but in practice,
the Oracle approach is more practical (i'm starting to sound like yogi
berra!)

if i'm creating a UNIQUE constraint, my goal is to disallow values that
are known to be equal to other known values -- so, it's entirely
appropriate to not disallow (sorry for the double-negs) unknown values
(triple-negs?) since there is no known basis for excluding them


But then why does it exclude them when there are other columns present?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Jul 19 '05 #8
"Agoston Bejo" <gu***@freemail .hu> wrote:
See the answer below.

"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote in message
news:zs******* *************@c omcast.com...
"Agoston Bejo" <gu***@freemail .hu> wrote in message
news:cl******** **@news.caesar. elte.hu...
| I want to enforce such a constraint on a column that would ensure that

the
| values be all unique, but this wouldn't apply to NULL values. (I.e.

there
| may be more than one NULL value in the column.)
| How can I achieve this?
| I suppose I would get the most-hated "table/view is changing,
| trigger/function may not see it" error if I tried to write a trigger

that
| checks the uniqueness of non-null values upon insert/update.
|
|

did you try a standard UNIQUE constraint on the column?

Yes, I did, and (1, null) and (1, null) couldn't be inserted twice. The
Oracle version I'm currently using (or to be more exact forced to use) is
8.1.7. Maybe in later versions this was corrected, I don't know. Here, when
I tried it, it worked the way I described in my original post.

unlike SQL-Server (unless they've changed it since I last worked on it),
Oracle processes null values properly in this scenario (i.e., one NULL

value
is never consider equal to another NULL value)

++ mcs

A Unique Index only allows for 1 NULL in each of the indexed fields..So
Insert 1,NULL
and
insert 1,NULL

would violate the unique index since the NULL in field2 is the second NULL and is not allowed..
You could do a
insert NULL,1 without a problem ( except now both fields have their max NULLs, so no more will be allowed)


Jul 19 '05 #9

"Turkbear" <jo****@dot.spa mfree.com> wrote in message
news:1098379134 .J+ht36vV5NqDSC hGoHyPcg@terane ws...
| "Agoston Bejo" <gu***@freemail .hu> wrote:
|
| >See the answer below.
| >
| >"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote in message
| >news:zs******* *************@c omcast.com...
| >> "Agoston Bejo" <gu***@freemail .hu> wrote in message
| >> news:cl******** **@news.caesar. elte.hu...
| >> | I want to enforce such a constraint on a column that would ensure
that
| >the
| >> | values be all unique, but this wouldn't apply to NULL values. (I.e.
| >there
| >> | may be more than one NULL value in the column.)
| >> | How can I achieve this?
| >> | I suppose I would get the most-hated "table/view is changing,
| >> | trigger/function may not see it" error if I tried to write a trigger
| >that
| >> | checks the uniqueness of non-null values upon insert/update.
| >> |
| >> |
| >>
| >> did you try a standard UNIQUE constraint on the column?
| >
| >
| >Yes, I did, and (1, null) and (1, null) couldn't be inserted twice. The
| >Oracle version I'm currently using (or to be more exact forced to use) is
| >8.1.7. Maybe in later versions this was corrected, I don't know. Here,
when
| >I tried it, it worked the way I described in my original post.
| >
| >>
| >> unlike SQL-Server (unless they've changed it since I last worked on
it),
| >> Oracle processes null values properly in this scenario (i.e., one NULL
| >value
| >> is never consider equal to another NULL value)
| >>
| >> ++ mcs
| >>
| >>
| >
| A Unique Index only allows for 1 NULL in each of the indexed fields..So
| Insert 1,NULL
| and
| insert 1,NULL
|
| would violate the unique index since the NULL in field2 is the second
NULL and is not allowed..
| You could do a
| insert NULL,1 without a problem ( except now both fields have their max
NULLs, so no more will be allowed)
i'm sure you were responding specifically to the issue multi-column unique
constraints (indexes) but just to make it clear for any neophytes listening
in:
this is legal (single-column unique constraint, multiple rows with NULL
value):
-----------------------------------------------------------------
SQL> create table uk_demo (
2 id number constraint uk_demo$pk primary key
3 , name varchar2(30) constraint uk_demp$uk unique
4 );

Table created.

SQL> insert into uk_demo values (1,null);

1 row created.

SQL> insert into uk_demo values (2,null);

1 row created.

SQL> insert into uk_demo values (3,null);

1 row created.
this is not (multi-column unique constraint, dups in non-null column(s)):
-----------------------------------------------------------------
SQL> create table uk_demo2(
2 id number constraint uk_demo2$pk primary key
3 , deptno number
4 , name varchar2(30)
5 , constraint uk_demo2$uk unique ( deptno, name )
6 );

Table created.

SQL> insert into uk_demo2 values(1,200,nu ll);

1 row created.

SQL> insert into uk_demo2 values(2,200,nu ll);
insert into uk_demo2 values(2,200,nu ll)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_DEMO2 $UK) violated
but this is (multi-column unique constraint, all columns null for multiple
rows):
-----------------------------------------------------------------
SQL> insert into uk_demo2 values(3,null,n ull);

1 row created.

SQL> insert into uk_demo2 values(4,null,n ull);

1 row created.
++ mcs
Jul 19 '05 #10

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

Similar topics

4
38545
by: Dave | last post by:
Can you create a unique constraint on multiple columns, or does it have to be implemented as a unique index? If possible can someone please post some sample code? Thanks,
3
22623
by: Prince Kumar | last post by:
Is there any way I can define an Unique constraint or unique index which allows more than one null values for the same column combination in DB2? ie, If my index is defined on (col3, col4) where both columns allow nulls, I want col3 + col4 to be unique, if one or both the columns have values. If both columns have nulls, it should allow more than one such rows. ex,
5
16703
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a unique index like:
10
14663
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
1
2079
by: D.Stone | last post by:
Hi, I'm getting hung up on a trivial schema change - I need to make a column accept only null or unique values and Books Online avers 'what you need is a UNIQUE constraint'. Fine. I've tried doing this interactively both in Access and in Enterprise Manager; I've deleted the column and recreated it; I've written a T- SQL script to ALTER TABLE. All fail with messages along the lines of: - Unable to create index 'IX_Student'. ODBC error:...
26
570
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.