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)

既複製表結構,也可以複製約束,索引等;但是不會複製數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章