Re: linking - Mailing list pgsql-novice

From Oliver Elphick
Subject Re: linking
Date
Msg-id 200103030221.f232LBG00549@linda.lfix.co.uk
Whole thread Raw
In response to linking  (si <s@remail.net>)
List 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.Elphick@lfix.co.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



pgsql-novice by date:

Previous
From: Designer
Date:
Subject: Removal from list
Next
From: Boulat Khakimov
Date:
Subject: How to Describe a field in the table???