sql question; checks if data already exists before inserted

From: "nuno" <wegein(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: sql question; checks if data already exists before inserted
Date: 2006-06-22 05:59:42
Message-ID: 1150955982.360851.98230@m73g2000cwd.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

hi, there. i'm trying to write a SQL statement which does the following
things.

1. checks if data already exists in the database
2. if not, insert data into database otherwise skip.

for example, i'd like to insert a student called 'Michael Jordan' whose
ID is 'JORDANMICHAEL' only if the id, 'JORDANMICHAEL' does not already
exist in the database. anyway, my query looks like...

insert into student (studentid, fname, lname)
select 'JORDANMICHAEL', 'Michale', 'Jordan' from student where
studentid not in
(select studentid from student);

however, this does not seem to work. it does not insert data even if it
does not exist in the database. hmm!

any clue?

thanks.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message zzzeek 2006-06-22 06:01:54 Re: best way to get PKey and FKey from inside applications (permission pb)
Previous Message John Tregea 2006-06-22 05:01:53 Re: Return the primary key of a newly inserted row?