pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedINSERT only under certain conditions (SELECT)
5+ messages / 3 participants
[nested] [flat]
* INSERT only under certain conditions (SELECT)
@ 2002-07-08 01:18 Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-08 08:12 ` Re: INSERT only under certain conditions (SELECT) Rajesh Kumar Mallah. <mallah@trade-india.com>
2002-07-08 12:40 ` Re: INSERT only under certain conditions (SELECT) Bruno Wolff III <bruno@wolff.to>
0 siblings, 2 replies; 5+ messages in thread
From: Joachim Trinkwitz @ 2002-07-08 01:18 UTC (permalink / raw)
To: pgsql-sql
Hi,
I want to insert a row only under condition that there isn't already
another row with similar values -- something like a INSERT INTO
... WHERE NOT EXISTS (SELECT ...)?
Hoping for help,
joachim
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: INSERT only under certain conditions (SELECT)
2002-07-08 01:18 INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
@ 2002-07-08 08:12 ` Rajesh Kumar Mallah. <mallah@trade-india.com>
1 sibling, 0 replies; 5+ messages in thread
From: Rajesh Kumar Mallah. @ 2002-07-08 08:12 UTC (permalink / raw)
To: Joachim Trinkwitz <jtr@uni-bonn.de>; pgsql-sql
create an uniq index on the columns,
# create unique index index_name on table (col1,col2,col3,...)
hope it helps.
regds
mallah
On Monday 08 July 2002 06:48, Joachim Trinkwitz wrote:
> Hi,
>
> I want to insert a row only under condition that there isn't already
> another row with similar values -- something like a INSERT INTO
> ... WHERE NOT EXISTS (SELECT ...)?
>
> Hoping for help,
> joachim
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
--
Rajesh Kumar Mallah,
Project Manager (Development)
Infocom Network Limited, New Delhi
phone: +91(11)6152172 (221) (L) ,9811255597 (M)
Visit http://www.trade-india.com ,
India's Leading B2B eMarketplace.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: INSERT only under certain conditions (SELECT)
2002-07-08 01:18 INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
@ 2002-07-08 12:40 ` Bruno Wolff III <bruno@wolff.to>
2002-07-08 15:49 ` Re: INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
1 sibling, 1 reply; 5+ messages in thread
From: Bruno Wolff III @ 2002-07-08 12:40 UTC (permalink / raw)
To: Joachim Trinkwitz <jtr@uni-bonn.de>; +Cc: pgsql-sql
On Mon, Jul 08, 2002 at 03:18:33 +0200,
Joachim Trinkwitz <jtr@uni-bonn.de> wrote:
> Hi,
>
> I want to insert a row only under condition that there isn't already
> another row with similar values -- something like a INSERT INTO
> ... WHERE NOT EXISTS (SELECT ...)?
You can do something like the following:
insert into tab (col1, col2, col3) select 'val1', 'val2', 'val3' where
not exists (select * from tab where col1 = 'val1' and col2 = 'val2' and
col3 = 'val3');
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: INSERT only under certain conditions (SELECT)
2002-07-08 01:18 INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-08 12:40 ` Re: INSERT only under certain conditions (SELECT) Bruno Wolff III <bruno@wolff.to>
@ 2002-07-08 15:49 ` Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-09 12:18 ` Re: INSERT only under certain conditions (SELECT) Bruno Wolff III <bruno@wolff.to>
0 siblings, 1 reply; 5+ messages in thread
From: Joachim Trinkwitz @ 2002-07-08 15:49 UTC (permalink / raw)
To: pgsql-sql
Maybe I should explain my problem a little bit: simplified, I have two
tables, L and T, (which values references to L):
L:
art|kvvnr|semester
---+-----+--------
7.0| 4151| 2002ws
7.0| 4326| 2002ws
6.1| 4200| 2002ws
7.0| 4151| 2001ws
(combination of kvvnr and semester is unique)
T:
id |semester|kvvnr
----+--------+-----
123| 2002ws |4151
123| 2001ws |4151
Now I want to allow a user with id 123 only to insert a row in T if he
doesn't choose values from L with same 'art' and 'semester' values --
in the examples user 123 has already chosen a kvvnr from semester
2002ws in category 'art', so he shouldn't be allowed to insert the
values of the second row in table L.
I hope, someone has followed this complicated thing up to here and
there is a solution for my problem.
Greetings and thanks for your answers by now and further on,
joachim
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: INSERT only under certain conditions (SELECT)
2002-07-08 01:18 INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-08 12:40 ` Re: INSERT only under certain conditions (SELECT) Bruno Wolff III <bruno@wolff.to>
2002-07-08 15:49 ` Re: INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
@ 2002-07-09 12:18 ` Bruno Wolff III <bruno@wolff.to>
0 siblings, 0 replies; 5+ messages in thread
From: Bruno Wolff III @ 2002-07-09 12:18 UTC (permalink / raw)
To: Joachim Trinkwitz <jtr@uni-bonn.de>; +Cc: pgsql-sql
On Mon, Jul 08, 2002 at 17:49:56 +0200,
Joachim Trinkwitz <jtr@uni-bonn.de> wrote:
>
> Now I want to allow a user with id 123 only to insert a row in T if he
> doesn't choose values from L with same 'art' and 'semester' values --
> in the examples user 123 has already chosen a kvvnr from semester
> 2002ws in category 'art', so he shouldn't be allowed to insert the
> values of the second row in table L.
In the database you enforce this using a unique index.
The application needs to be able to handle failures when you try to
insert a duplicate key.
If you can't afford to have a transaction fail part way through, then
anything that writes that table should do a table lock at the start
of the transaction.
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2002-07-09 12:18 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-08 01:18 INSERT only under certain conditions (SELECT) Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-08 08:12 ` Rajesh Kumar Mallah. <mallah@trade-india.com>
2002-07-08 12:40 ` Bruno Wolff III <bruno@wolff.to>
2002-07-08 15:49 ` Joachim Trinkwitz <jtr@uni-bonn.de>
2002-07-09 12:18 ` Bruno Wolff III <bruno@wolff.to>
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