Unique Indexes

From: "ILove TheSpam" <ilovethespam(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Unique Indexes
Date: 2005-03-11 16:21:49
Message-ID: BAY9-F18A7235C27C2E5AA148B23C0530@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I'm new to Postgres and plpgsql so my sincerest apologies if the answer to
my problem is common :-)
I want to add entries to a table. The table has 2 fields, FieldID which is
the primary key and FieldValue which is a unique field.
I can add to FieldValue just fine when the unique constraint hasn't been
breached. However, if I try to add to FieldValue and there's already another
field with that value, obviously I'll get an error.
My problem is that I want to:
a) Be able to detect when an error occurs and be able to read that error
b) Find out the FieldID for the row which already has the value in it. So if
I try to enter 'blah' into FieldValue and it's already in the row with
FieldID=3, I want the value 3 to be returned to me. I realise this can be
done using a SELECT after the error, but I'd like to avoid that. I'm hoping
Postgres itself has some way of detecting this sort of thing.

Even though I'm new to Postgres, I have looked at triggers and functions and
I'm feeling fairly confident with them, so please, if that's what it takes,
suggest it :-)

Thanks!

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2005-03-11 16:31:46 Re: Best practices: Handling Daylight-saving time
Previous Message Martijn van Oosterhout 2005-03-11 16:20:08 Re: Best practices: Handling Daylight-saving time