开发者

alter table to create columns for other table

开发者 https://www.devze.com 2023-01-05 15:25 出处:网络
can anyone help me with creating column for table A that are from table B ? let\'s say i have table B:

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 VI开发者_运维问答EW

thanks


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;


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

精彩评论

暂无评论...
验证码 换一张
取 消