postgres创建和数据表相同结构的表的方法

来源:互联网 发布:微软人工智能培训机构 编辑:程序博客网 时间:2024/06/06 02:41
highgo=# select * from test;
  id  
------
    1
    1
    1
    1
    1
  100
  200
  300
  400
  400
 1000
 2000
 3000
 3000
 3000
    1
(16 rows)


highgo=# create table testa as select * from test;    -此方式即有表结构又包含数据
SELECT 16
highgo=# create table testb (like test);    --此方式仅创建表结构,没有数据
CREATE TABLE
highgo=# 
highgo=# select * from testa;
  id  
------
    1
    1
    1
    1
    1
  100
  200
  300
  400
  400
 1000
 2000
 3000
 3000
 3000
    1
(16 rows)


highgo=# select * from testb;
 id 
----
(0 rows)


highgo=# create table testc as select * from test with no data;  ---此方式效果和like方式相同 
CREATE TABLE AS
highgo=# 
highgo=# select * from testc;
 id 
----
(0 rows)




使用like方式时,有如下选项:
highgo=# create table testc as select * from test with no data including all;
错误:  语法错误 在 "including" 或附近的
LINE 1: ...te table testc as select * from test with no data including ...
                                                             ^
highgo=# 
highgo=# create table testd (like test) including all;
错误:  语法错误 在 "including" 或附近的
LINE 1: create table testd (like test) including all;
                                       ^
highgo=# create table testd (like test including all);
CREATE TABLE
including default
including constraints
including indexes
including storage
including comments
including all---把所有属性都复制过去
原创粉丝点击