Re: linking

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: s(at)remail(dot)net
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: linking
Date: 2001-03-03 02:21:10
Message-ID: 200103030221.f232LBG00549@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

si wrote:
>Please can you help:

>Insert into DEPARTMENT values ('Bedrock','Administration');
>
>Insert into Employee values ('1','Slate','????');
>
>How do I replace ???? to automatically get the DEPT_NO value?
>
>e.g. In theory I need to replace ???? with (select DEPT_NO from DEPARTMENT w
>here location = 'Administration' and DESCRIPTION = 'Bedrock';) but I am
>not sure how to do this all in 1 statement.
>
>The DEPT_NO is automatically created if no value is given, which is why I al
>so need to create the DEPT relation automatically.

In 7.1 you can simply do this:

junk=# Insert into Employee values (1,'Slate',
junk(# (select DEPT_NO from DEPARTMENT where DESCRIPTION = 'Administration'
and location = 'Bedrock'));
INSERT 2085500 1

junk=# select * from department;
location | description | dept_no
----------+----------------+---------
Bedrock | Administration | 1
(1 row)

junk=# select * from employee;
empl_id | name_last | dept
---------+-----------+------
1 | Slate | 1
(1 row)

However, if you do this, you need a unique index on (location, description);
at the moment you could have more than one dept_no for the same combination.

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"I will lift up mine eyes unto the hills, from whence
cometh my help. My help cometh from the LORD, which
made heaven and earth."
Psalms 121:1,2

Browse pgsql-novice by date

  From Date Subject
Next Message Boulat Khakimov 2001-03-04 16:40:17 How to Describe a field in the table???
Previous Message Designer 2001-03-03 02:14:13 Removal from list