如何在psql中使用外键约束将一个表的结构复制到另一个表中? [英] How to copy structure of one table to another with foreign key constraints in psql?

查看:336
本文介绍了如何在psql中使用外键约束将一个表的结构复制到另一个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

  create table table_name(与source_table包含所有内容相同)'

在Postgres中。如何创建包含所有外键的现有表的副本

解决方案

无法自动创建外键 CREATE TABLE ... LIKE ...



对于文档:
$ b


LIKE source_table [like_option ...]

非空约束总是被复制到新表中。 CHECK只有在指定了INCLUDING CONSTRAINTS的情况下,
约束才会被复制[/ b]
$ b $原始表中的索引,PRIMARY KEY和UNIQUE约束$ b $只有在指定了INCLUDING INDEXES子句
时,才会在新表上创建b。

实际上,使用GUI工具。例如,在PgAdmin III中:


  • source_table 的复制声明(DDL)查询工具(ctrl-e),
  • 编辑声明,
  • 执行sql。





在SQL脚本中,您可以使用以下函数。重要的假设:源表外键具有正确的名称,即它们的名称包含源表名称(典型的情况是什么)。

$ p $ create或者替换函数create_table_like(source_table text,new_table text)
返回void language plpgsql
as $$
declare
rec record;
begin
执行格式(
'create table%s(like%s including all)',
new_table,source_table);
for rec in
选择oid,conname
从pg_constraint
其中contype ='f'
和conrelid = source_table :: regclass
循环
执行格式(
'alter table%s add add%s%s',
new_table,
replace(rec.conname,source_table,new_table),
pg_get_constraintdef(rec。 OID));
结束循环;
end $$;

使用示例:

  create table base_table(base_id int primary key); 
create table source_table(id int primary key,base_id int references base_table);

选择create_table_like('source_table','new_table');

\ new_table

表public.new_table
列|类型|修饰符
--------- + --------- + -----------
id |整数|不为空
base_id |整数|
索引:
new_table_pkeyPRIMARY KEY,btree(id)
外键约束:
new_table_base_id_fkeyFOREIGN KEY(base_id)REFERENCES base_table(base_id)


Foreign key constraints are not copied when using

create table table_name ( like source_table INCLUDING ALL)' 

in Postgres. How can I create a copy of an existing table including all foreign keys.

解决方案

There is no option to automatically create foreign keys in CREATE TABLE ... LIKE ....

For the documentation:

LIKE source_table [ like_option ... ]

Not-null constraints are always copied to the new table. CHECK constraints will be copied only if INCLUDING CONSTRAINTS is specified [...]

Indexes, PRIMARY KEY, and UNIQUE constraints on the original table will be created on the new table only if the INCLUDING INDEXES clause is specified.

In practice it's easy with GUI tools. For example, in PgAdmin III:

  • copy declaration (DDL) of source_table to query tool (ctrl-e),
  • edit the declaration,
  • execute sql.

In an SQL script you can use the following function. Important assumption: source table foreign keys have correct names i.e. their names contain source table name (what is a typical situation).

create or replace function create_table_like(source_table text, new_table text)
returns void language plpgsql
as $$
declare
    rec record;
begin
    execute format(
        'create table %s (like %s including all)',
        new_table, source_table);
    for rec in
        select oid, conname
        from pg_constraint
        where contype = 'f' 
        and conrelid = source_table::regclass
    loop
        execute format(
            'alter table %s add constraint %s %s',
            new_table,
            replace(rec.conname, source_table, new_table),
            pg_get_constraintdef(rec.oid));
    end loop;
end $$;

Example of use:

create table base_table (base_id int primary key);
create table source_table (id int primary key, base_id int references base_table);

select create_table_like('source_table', 'new_table');

\d new_table

   Table "public.new_table"
 Column  |  Type   | Modifiers 
---------+---------+-----------
 id      | integer | not null
 base_id | integer | 
Indexes:
    "new_table_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "new_table_base_id_fkey" FOREIGN KEY (base_id) REFERENCES base_table(base_id)

这篇关于如何在psql中使用外键约束将一个表的结构复制到另一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆