fazo
fazo

Reputation: 1827

alter table to create columns for other table

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

Upvotes: 2

Views: 452

Answers (2)

tinychen
tinychen

Reputation: 2099

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);

Upvotes: 0

rfusca
rfusca

Reputation: 7705

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;

Upvotes: 2

Related Questions