If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Unique Constraint

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-11, 14:00
valandil valandil is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Unique Constraint

Hello!

I want to implement something that will act as a UNIQUE constraint on a table, but I need a little more flexibility.

I want to implement a three-column UNIQUE constraint. However, one of the columns may be NULLs and I would want, essentially, that the NULLs be considered equal amongst them.

Each of the columns define part of a scheme to identify objects. For instance, a row could have the values (1, 3, NULL). This would translate to a CQp.kVI.3, for instance. On the other hand, I don't want another (1,3,NULL) row to be added to the DB.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 08-25-11, 10:02
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
I believe you've got two approaches you could take: add either a before insert/update trigger, or a rule, to monitor inserts/updates to table.field2, and when the count of records with field2 is null (and field1 = new.field1) then raise an error if using a trigger, or an 'Instead do nothing' if using rules.

Although I've needed to use it very little, the rule system is the preferred approach when it will work for you.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #3 (permalink)  
Old 08-26-11, 10:54
valandil valandil is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Thank you! I'll try the rules system.
Reply With Quote
Reply

Tags
null values, unique constraint

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On