PostgreSQL 使用其他表作为模板创建新表

使用其他表作为模板创建新表

创建表tbl_big 并利用该表为模板,快速创建表 ( like tbl_big including all )

[root@uzong ~]# su postgres
bash-4.2$ psql technology
could not change directory to "/root"
psql (9.2.24, server 10.8)
WARNING: psql version 9.2, server version 10.0.
         Some psql features might not work.
Type "help" for help.

technology=# create table tbl_big (
technology(# id int primary key, 
technology(# info text, 
technology(# crt_time timestamp
technology(# );
CREATE TABLE
technology=# create index idx_tbl_big on tbl_big (crt_time); 
CREATE INDEX
technology=# \d tbl_big;
               Table "public.tbl_big"
  Column  |            Type             | Modifiers 
----------+-----------------------------+-----------
 id       | integer                     | not null
 info     | text                        | 
 crt_time | timestamp without time zone | 
Indexes:
    "tbl_big_pkey" PRIMARY KEY, btree (id)
    "idx_tbl_big" btree (crt_time)

technology=# create table tbl ( like tbl_big including all );
CREATE TABLE
technology=# \d tbl;
                 Table "public.tbl"
  Column  |            Type             | Modifiers 
----------+-----------------------------+-----------
 id       | integer                     | not null
 info     | text                        | 
 crt_time | timestamp without time zone | 
Indexes:
    "tbl_pkey" PRIMARY KEY, btree (id)
    "tbl_crt_time_idx" btree (crt_time)

通过( like tbl_big including all ) 可以快速复制一个表解构(包括索引,主键等各种信息)

更多的参数:

including defults

including constraints

including indexes

including storage

including comments

including all  -- 全部

总结

关于使用其他表作为模板创建新的表适用场景,适用于继承、分表,复制等情况

发布了274 篇原创文章 · 获赞 119 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/qq_31156277/article/details/98516962