PG 表的复制

create as

postgres=# create table test_as as select * from test where 1=0;
SELECT 0
postgres=# \d+ test_as
                                               Table "public.test_as"
   Column    |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id          | integer                     |           |          |         | plain    |              | 
 name        | character varying(10)       |           |          |         | extended |              | 
 age         | bigint                      |           |          |         | plain    |              | 
 create_time | timestamp without time zone |           |          |         | plain    |              | 
Access method: heap

只复制表结构,不复制约束

create like

DROP TABLE
postgres=# create table test(id int primary key, name varchar(10), age int8, create_time timestamp without time zone );
CREATE TABLE
postgres=# 
postgres=# insert into test values(1,'a',18,now());
INSERT 0 1
postgres=# select * from test;
 id | name | age |        create_time         
----+------+-----+----------------------------
  1 | a    |  18 | 2020-06-11 18:50:30.851164
(1 row)

postgres=# create table test_bak (like test);
CREATE TABLE
postgres=# 
postgres=# \d+ test
                                                Table "public.test"
   Column    |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id          | integer                     |           | not null |         | plain    |              | 
 name        | character varying(10)       |           |          |         | extended |              | 
 age         | bigint                      |           |          |         | plain    |              | 
 create_time | timestamp without time zone |           |          |         | plain    |              | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)
Access method: heap

postgres=# \d+ test_bak
                                              Table "public.test_bak"
   Column    |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id          | integer                     |           | not null |         | plain    |              | 
 name        | character varying(10)       |           |          |         | extended |              | 
 age         | bigint                      |           |          |         | plain    |              | 
 create_time | timestamp without time zone |           |          |         | plain    |              | 
Access method: heap

只复制了表结构,但是没有复制约束

create like including all

DROP TABLE
postgres=# create table test(id int primary key, name varchar(10), age int8, create_time timestamp without time zone );
CREATE TABLE
postgres=# 
postgres=# create index idx_test_name on test using btree(name);
CREATE INDEX
postgres=# 
postgres=# \d+ test
                                                Table "public.test"
   Column    |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id          | integer                     |           | not null |         | plain    |              | 
 name        | character varying(10)       |           |          |         | extended |              | 
 age         | bigint                      |           |          |         | plain    |              | 
 create_time | timestamp without time zone |           |          |         | plain    |              | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)
    "idx_test_name" btree (name)
Access method: heap

postgres=# insert into test values(1,'a',10,now());
INSERT 0 1
postgres=# create table test_bak_including_all ( like test including all);
CREATE TABLE
postgres=# \d+ test_bak_including_all
                                       Table "public.test_bak_including_all"
   Column    |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id          | integer                     |           | not null |         | plain    |              | 
 name        | character varying(10)       |           |          |         | extended |              | 
 age         | bigint                      |           |          |         | plain    |              | 
 create_time | timestamp without time zone |           |          |         | plain    |              | 
Indexes:
    "test_bak_including_all_pkey" PRIMARY KEY, btree (id)
    "test_bak_including_all_name_idx" btree (name)
Access method: heap
postgres=# select * from test;
 id | name | age |        create_time         
----+------+-----+----------------------------
  1 | a    |  10 | 2020-06-11 19:05:10.775363
(1 row)

postgres=# select * from test_bak_including_all ;
 id | name | age | create_time 
----+------+-----+-------------
(0 rows)

既复制表结构,也可以复制约束,索引等;但是不会复制数据

你可能感兴趣的:(PostgreSQL)