pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Cristóvão B. B. Dalla Costa <cdallacosta@bigfoot.com>
To: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Conditional SQL query
Date: Sun, 5 Nov 2000 19:44:53 -0300
Message-ID: <003f01c0477a$04717320$02ffa8c0@terrificus> (raw)
References: <Pine.SGI.3.96.1001106015029.1002323A-100000@www.cdfd.org.in>

>
> 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).





view thread (10+ messages)  latest in thread

Message-ID: <003f01c0477a$04717320$02ffa8c0@terrificus>
Permalink:  ../003f01c0477a$04717320$02ffa8c0@terrificus/
Also on:    postgresql.org/message-id/003f01c0477a$04717320$02ffa8c0@terrificus

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: cdallacosta@bigfoot.com
  Subject: Re: Conditional SQL query
  In-Reply-To: <003f01c0477a$04717320$02ffa8c0@terrificus>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox