pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
insert related data into two tables
3+ messages / 3 participants
[nested] [flat]

* insert related data into two tables
@ 2006-05-14 21:40 filippo2991@virgilio.it
  2006-05-21 00:54 ` Re: insert related data into two tables Ash Grove <ash_grv7@yahoo.com>
  2006-05-22 09:31 ` Re: insert related data into two tables Patrick JACQUOT <patrick.jacquot@anpe.fr>
  0 siblings, 2 replies; 3+ messages in thread

From: filippo2991@virgilio.it @ 2006-05-14 21:40 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I have two tables like these:

TABLE_1:  people registry
fields: ID_T1, SURNAME, NAME
ID_T1 is primary key

TABLE_2: work groups
fields: ID_T2, TASK
ID_T2 is foreign key related to ID_T1

the first table is the list of employees, the second the task.

Sometime I need to insert a new employee and at the same time the task
for him:
I want to INSERT TO table 1 JOHN DOE (the ID is assigned automatically
since it's a primary key) and INSERT TO table 2 the ID and the task
name for JOHN DOE. Is it possible to create a single query? What is the
best way to do this?

Thanks,

Filippo




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: insert related data into two tables
  2006-05-14 21:40 insert related data into two tables filippo2991@virgilio.it
@ 2006-05-21 00:54 ` Ash Grove <ash_grv7@yahoo.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Ash Grove @ 2006-05-21 00:54 UTC (permalink / raw)
  To: pgsql-sql

You need to manage the transaction: begin a
transaction, execute your first insert, retrieve the
ID with curr_val(), execute the sencond insert and
commit the transaction.

--- filippo2991@virgilio.it wrote:

> Hello,
> 
> I have two tables like these:
> 
> TABLE_1:  people registry
> fields: ID_T1, SURNAME, NAME
> ID_T1 is primary key
> 
> TABLE_2: work groups
> fields: ID_T2, TASK
> ID_T2 is foreign key related to ID_T1
> 
> the first table is the list of employees, the second
> the task.
> 
> Sometime I need to insert a new employee and at the
> same time the task
> for him:
> I want to INSERT TO table 1 JOHN DOE (the ID is
> assigned automatically
> since it's a primary key) and INSERT TO table 2 the
> ID and the task
> name for JOHN DOE. Is it possible to create a single
> query? What is the
> best way to do this?
> 
> Thanks,
> 
> Filippo
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faq
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: insert related data into two tables
  2006-05-14 21:40 insert related data into two tables filippo2991@virgilio.it
@ 2006-05-22 09:31 ` Patrick JACQUOT <patrick.jacquot@anpe.fr>
  1 sibling, 0 replies; 3+ messages in thread

From: Patrick JACQUOT @ 2006-05-22 09:31 UTC (permalink / raw)
  To: filippo2991@virgilio.it; +Cc: pgsql-sql

filippo2991@virgilio.it wrote:

>Hello,
>
>I have two tables like these:
>
>TABLE_1:  people registry
>fields: ID_T1, SURNAME, NAME
>ID_T1 is primary key
>
>TABLE_2: work groups
>fields: ID_T2, TASK
>ID_T2 is foreign key related to ID_T1
>
>the first table is the list of employees, the second the task.
>
>Sometime I need to insert a new employee and at the same time the task
>for him:
>I want to INSERT TO table 1 JOHN DOE (the ID is assigned automatically
>since it's a primary key) and INSERT TO table 2 the ID and the task
>name for JOHN DOE. Is it possible to create a single query? What is the
>best way to do this?
>
>Thanks,
>
>Filippo
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: Have you checked our extensive FAQ?
>
>               http://www.postgresql.org/docs/faq
>
>  
>
maybe you could create a view for the join of the two tebles,
then make that view writable by creating the
appropriate triggers.
Then a single insert into the view would in fact create everything.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2006-05-22 09:31 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-05-14 21:40 insert related data into two tables filippo2991@virgilio.it
2006-05-21 00:54 ` Ash Grove <ash_grv7@yahoo.com>
2006-05-22 09:31 ` Patrick JACQUOT <patrick.jacquot@anpe.fr>

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