Re: Conditional SQL query - Mailing list pgsql-sql

From Cristóvão B. B. Dalla Costa
Subject Re: Conditional SQL query
Date
Msg-id 003f01c0477a$04717320$02ffa8c0@terrificus
Whole thread Raw
In response to Re: Conditional SQL query  (Indraneel Majumdar <indraneel@www.cdfd.org.in>)
List 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).




pgsql-sql by date:

Previous
From: Indraneel Majumdar
Date:
Subject: Re: Conditional SQL query
Next
From: Indraneel Majumdar
Date:
Subject: Re: Conditional SQL query