Unique Indexes - Mailing list pgsql-general

From ILove TheSpam
Subject Unique Indexes
Date
Msg-id BAY9-F18A7235C27C2E5AA148B23C0530@phx.gbl
Whole thread Raw
Responses Re: Unique Indexes  (Michael Fuhr <mike@fuhr.org>)
List 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/


pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Best practices: Handling Daylight-saving time
Next
From: "Magnus Hagander"
Date:
Subject: Re: VMWare and PostgreSQL: WAS :PostgreSQL still for Linux only?