473,545 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

disallowing multiple NULLs in a unique constraint

SQL Server only allow one NULL in a unique constraint column (it's the
unique index that does that, so the unique constraint behaves like that
too). The question is, what is the best way to simulate that behaviour
in Postgres? Can a CHECK constraint does that? Will a trigger with
SELECT count(*) ... WHERE f IS NULL be too slow if the table is large?

--
dave
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #1
4 6513
David Garamond <li***@zara.6.i sreserved.com> writes:
SQL Server only allow one NULL in a unique constraint column (it's the
unique index that does that, so the unique constraint behaves like that
too). The question is, what is the best way to simulate that behaviour
in Postgres?


The best way is to rewrite your app to not depend on nonstandard
semantics. SQL Server is unquestionably violating the SQL spec here,
and it's not out of the question that Microsoft might realize that and
fix it, leaving you up the creek on that platform as well as Postgres.

Instead of using NULL in that fashion, perhaps you could choose a
non-null dummy value to use instead.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #2
Tom Lane wrote:
SQL Server only allow one NULL in a unique constraint column (it's the
unique index that does that, so the unique constraint behaves like that
too). The question is, what is the best way to simulate that behaviour
in Postgres?


The best way is to rewrite your app to not depend on nonstandard
semantics. SQL Server is unquestionably violating the SQL spec here,
and it's not out of the question that Microsoft might realize that and
fix it, leaving you up the creek on that platform as well as Postgres.

Instead of using NULL in that fashion, perhaps you could choose a
non-null dummy value to use instead.


Thanks, Tom. Yeah, that answer was expected :-) DB2 is also violating
specs here, though they have a syntax (UNIQUE WHERE NOT NULL or something).

Btw, one example case: a table containing a tree with adjacency list.
The root node is the one that doesn't have a parent (parent_id is NULL).
parent_id REFERENCES thetable(id). How do we restrict the table to have
only one root node?

--
dave
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #3
Am Montag, 9. Februar 2004 06:06 schrieb David Garamond:
SQL Server only allow one NULL in a unique constraint column (it's the
unique index that does that, so the unique constraint behaves like that
too). The question is, what is the best way to simulate that behaviour
in Postgres? Can a CHECK constraint does that? Will a trigger with
SELECT count(*) ... WHERE f IS NULL be too slow if the table is large?


You can time that yourself (but I suspect it won't be pretty), but I think
that you're going to have all kinds of other problems if you base your
database schema on the assumption that NULL = NULL. Better fix the
application.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
On Wed, 12 May 2004, Peter Eisentraut wrote:
Am Montag, 9. Februar 2004 06:06 schrieb David Garamond:
SQL Server only allow one NULL in a unique constraint column (it's the
unique index that does that, so the unique constraint behaves like that
too). The question is, what is the best way to simulate that behaviour
in Postgres? Can a CHECK constraint does that? Will a trigger with
SELECT count(*) ... WHERE f IS NULL be too slow if the table is large?


You can time that yourself (but I suspect it won't be pretty), but I think
that you're going to have all kinds of other problems if you base your
database schema on the assumption that NULL = NULL. Better fix the
application.


It's important to make the point here that SQL Server is broken in this
respect, both in terms of set theory, and the SQL spec definition of how
NULLs behave.

The better option would be to use something other than NULL for this.
I.e. use the characters 'N/A' or something.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5

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

Similar topics

8
7544
by: vishal | last post by:
i have one form on which the data is stored in database and suppose the user clicks the back button of browser and again click on submit button then the data will be saved in database again. so how can i solve this problem???? thxs for help in advance
1
2539
by: John Smith | last post by:
I have a user assigned multiple roles and a role can be inherited from multiple parents (see below). How do I answer such questions as "How many roles does the user belongs to?" I answered the above questions by using .NET but I think it can be more efficient by using just SQL. I would appreciate if you can give me an answer. Thank you.
5
10849
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
3
1802
by: noelwatson | last post by:
I am looking to create a constraint on a table that allows multiple nulls but all non-nulls must be unique. I found the following script http://www.windowsitpro.com/Files/09/21293/Listing_01.txt that works fine, but the following line CREATE UNIQUE CLUSTERED INDEX idx1 ON v_multinulls(a)
1
2024
by: mike | last post by:
Hello all, any advice or links to pages about how to have multiple language asp pages? For example, you have a database driven website that you want customers around the world to use. How do you setup the asp pages so that it is easy to change the text based on where the user is connecting from? Thanks, Mike
4
38539
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
2541
by: gregory.sharrow | last post by:
I need to secure a datawarehouse table at the row level based on 1 to many keys on that table. A user should only see the rows they have access to. I need to be able to figure out which rows they have access to using a single sql statement and it cannot be dynamic SQL or a stored procedure (this is a limitation based on the reporting tool we...
5
16695
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
14654
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
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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...
0
3466
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...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.