Thread: Related tables

Related tables

From
"Franco Cazzaro"
Date:
Hi,
 
First sorry for my elementary english.
 
I'm new in Postgre and, naturally need help.
 
Can anyones courtesely help in this simplified relaction?
 
 
table T1(
    ID serial primary key,
    NAME text unique,
    morefields......
);
 
 
table T2(
    oID int4,         --join to T1.ID
    NOTE text
    );
 
Now, extraction is very easy:
select NAME,NOTE from T1, T2 where T1,ID= T2.oID;
 
but, how can I insert a NOTE of a NAME automatically inserting NAME in T1 if does not exists ?
 
I'm thinking about a function that:
 
    function TI_ID( text )
    RESULTS int4
           IF  new.NAME does not exists in T1, THEN insert new.NAME and return the related new.ID
           ELSE returns the related ID of existing NAME.
   language ......;
 
Please, I'm looking for simple sintax.
 
 
Thanks for time.
Ciao.