Tutorial :alter table to create columns for other table



Question:

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


Solution:1

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;  


Solution:2

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »