Re: how to INSERT foreign keys? - Mailing list pgsql-novice

From Josh Berkus
Subject Re: how to INSERT foreign keys?
Date
Msg-id web-690491@davinci.ethosmedia.com
Whole thread Raw
In response to how to INSERT foreign keys?  (Phil Mitchell <phil.mitchell@pobox.com>)
List pgsql-novice
Phil,

> I have an <items> table, and a <keys> table, and a map table that
>  relates item_id's to key_id's. How do I populate item_keys with
>  data?

> INSERT INTO item_keys SELECT item_id from items where item = 'item
>  1', SELECT key_id from keys where key = 'key 1';

Hmmm ... a shortcut?  OK:

INSERT INTO item_keys (item_id, key_id)
SELECT item_id, key_id
FROM items, keys
--OR:  FROM items CROSS JOIN keys
WHERE item = 'item 1' AND key='key 1';

If you're absolutely sure that there is only one Key 1 and only one
 Item 1 in the table.

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: C Function Question
Next
From: Jason Earl
Date:
Subject: Re: [Novice] Create Table