tags:

views:

89

answers:

2

Hi,

can anyone help me with creating column for table A that are from table B ?

let's say i have table B:

column name:WORDS
      row 1:ONE
      row 2:TWO
      row 3:THREE

and want to have table A:

column name: ONE | TWO | THREE

I need this to be created and not some VIEW

thanks

+2  A: 

Something like...

create function create_my_table () as 
$$
declare
v_t text[];
begin
select array_agg(distinct quote_ident(words) ||' text' ) into v_t from table_B;
EXECUTE  'CREATE TABLE tableA (' || array_to_string(v_t,',') ||' );';
end;
$$ language plpgsql;

select create_my_table;
rfusca
thanks.. seems to do the job, but i'll stick to create tables with php if it has to look like that.
fazo
In Postgresql 9.0 (due out in less than a month)....this would be reduced to (not much better, but some)...DO$$EXECUTE 'CREATE TABLE tableA (' || (SELECT string_agg(distinct quote_ident(words) || ' text',',') from table_B) ||' )';$$ LANGUAGE plpgsql;
rfusca
A: 

You can use PostgreSQL's INHERITS syntax:

-- create tableB as a template

create table tableB(one varchar,two varchar,three varchar);

-- create tableA

create table tableA() INHERITS(tableB);
tinychen