Re: Conditional SQL query

From: Cristóvão B(dot) B(dot) Dalla Costa <cdallacosta(at)bigfoot(dot)com>
To: "pgsql-sql" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Conditional SQL query
Date: 2000-11-05 22:44:53
Message-ID: 003f01c0477a$04717320$02ffa8c0@terrificus
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
> Now I have one more problem. How do I insert only once if value does not
> exist? eg:
>

You should create a unique index on the appropriate fields. For example:

CREATE UNIQUE INDEX indexname ON table (field1, field2)

Then the insert will fail if this particular combination of the values of
(field1, field2) is already in the table.

Be careful if the insert is inside a transaction, tough. If it fails due to
non-uniqueness your transaction will be aborted (all further queries ignored
untill a COMMIT or ROLLBACK).

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Marc Rohloff 2000-11-06 08:15:07 Returning Recordsets from Stored-procs
Previous Message Cristóvão B. B. Dalla Costa 2000-11-05 21:46:07 Re: Conditional SQL query