postgresql----根据现有表创建新表
除普通的建表语句"create table table_name(columns);"之外,还可以根据现有表快速的创建新表:
一.使用create table ... (like ...)创建一个和原表结构相同的新表,根据INCLUDING保留原表的约束,索引等。
create table table_name (like parent_table {INCLUDING|EXCLUDING}{DEFAULTS|CONSTRAINTS|INDEXES|STORAGE|COMMENTS|ALL});
直接使用LIKE不使用INCLUDING,新表只保留原表的表结构和NOT NULL约束,但是使用INCLUDING CONSTRAINTS配置会保留了主键,唯一键,CHECK约束,并不会保留外键约束。
示例:
1.创建示例表
test=# create table tbl_inherits_test (a int not null);
CREATE TABLE
test=# alter table tbl_inherits_test add constraint pk_tbl_inherits_test_a primary key(a);
ALTER TABLEtest=# create table tbl_inherits_parent(
a int not null,
b varchar(32) not null default 'got u',
c int,
d date); test=# alter table tbl_inherits_parent add constraint pk_tbl_inherits_parent_a primary key(a);
ALTER TABLE
test=# alter table tbl_inherits_parent add constraint fk_tbl_inherits_parent_a foreign key(a) references tbl_inherits_test(a);
ALTER TABLE
test=# alter table tbl_inherits_parent add constraint ck_tbl_inherits_parent_c check (c>10);
ALTER TABLE
test=# alter table tbl_inherits_parent add constraint uk_tbl_inherits_parent_b_d unique (b,d);
ALTER TABLE
test=# create index idx_tbl_inherits_parent_d on tbl_inherits_parent using btree (d);
CREATE INDEX
2.使用LIKE创建表
test=# create table tbl_inherits_partition (like tbl_inherits_parent including constraints including indexes including defaults);
CREATE TABLE test=# \d tbl_inherits_parent
Table "public.tbl_inherits_parent"
Column | Type | Modifiers
--------+-----------------------+---------------------------------------------
a | integer | not null
b | character varying(32) | not null default 'got u'::character varying
c | integer |
d | date |
Indexes:
"pk_tbl_inherits_parent_a" PRIMARY KEY, btree (a)
"uk_tbl_inherits_parent_b_d" UNIQUE CONSTRAINT, btree (b, d)
"idx_tbl_inherits_parent_d" btree (d)
Check constraints:
"ck_tbl_inherits_parent_c" CHECK (c > 10)
Foreign-key constraints:
"fk_tbl_inherits_parent_a" FOREIGN KEY (a) REFERENCES tbl_inherits_test(a) test=# \d tbl_inherits_partition
Table "public.tbl_inherits_partition"
Column | Type | Modifiers
--------+-----------------------+---------------------------------------------
a | integer | not null
b | character varying(32) | not null default 'got u'::character varying
c | integer |
d | date |
Indexes:
"tbl_inherits_partition_pkey" PRIMARY KEY, btree (a)
"tbl_inherits_partition_b_d_key" UNIQUE CONSTRAINT, btree (b, d)
"tbl_inherits_partition_d_idx" btree (d)
Check constraints:
"ck_tbl_inherits_parent_c" CHECK (c > 10)
二、使用create table ... as table ... with {data|no data}创建一个和原表结构相同的新表,保留或不保留数据,但是不会继承原表的约束,索引等。
test=# insert into tbl_inherits_test values (1);
INSERT 0 1
test=# insert into tbl_inherits_parent (a,b,c,d) values(1,'sss',12,'2016-06-22 17:00:00');
INSERT 0 1
test=#
test=# create table tbl_inherits_partition1 as table tbl_inherits_parent with data;
SELECT 1
test=# select * from tbl_inherits_partition1 ;
a | b | c | d
---+-----+----+------------
1 | sss | 12 | 2016-06-22
(1 row) test=# \d tbl_inherits_partition1
Table "public.tbl_inherits_partition1"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer |
b | character varying(32) |
c | integer |
d | date | test=#
test=#
test=# create table tbl_inherits_partition2 as table tbl_inherits_parent with no data;
SELECT 0
test=# \d tbl_inherits_partition2
Table "public.tbl_inherits_partition2"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer |
b | character varying(32) |
c | integer |
d | date | test=# select * from tbl_inherits_partition2;
a | b | c | d
---+---+---+---
(0 rows) test=#
三、使用select * into new_table from table将结果集保存在新表中,但是只能执行一次。
test=# select * into tbl_inherits_partition3 from tbl_inherits_parent ;
SELECT 1
test=# \d tbl_inherits_partition3
Table "public.tbl_inherits_partition3"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer |
b | character varying(32) |
c | integer |
d | date | test=# select * from tbl_inherits_partition3 ;
a | b | c | d
---+-----+----+------------
1 | sss | 12 | 2016-06-22
(1 row)
四、使用create table new_table as select * from table将结果集保存在新表中。
test=# create table tbl_inherits_partition4 as select * from tbl_inherits_parent ;
SELECT 1
test=# \d tbl_inherits_partition4
Table "public.tbl_inherits_partition4"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer |
b | character varying(32) |
c | integer |
d | date | test=# select * from tbl_inherits_partition4 ;
a | b | c | d
---+-----+----+------------
1 | sss | 12 | 2016-06-22
(1 row)
postgresql----根据现有表创建新表的更多相关文章
- Sql中根据旧表创建新表的SQL语句
今天在网上查了下,根据旧表创建新表的SQL语句,网上给了两个答案 create table tab_new like tab_old (使用旧表创建新表) create table tab_new a ...
- php大力力 [023节]CREATE TABLE创建新表sql写字段备注(2015-08-27)
2015-08-27 php大力力023.CREATE TABLE创建新表sql写字段备注 http://www.cnblogs.com/dalitongxue/p/4762182.html 参考: ...
- C#使用SqlBulkCopy将DataTable写入数据库的表中(表不存在则创建新表,数据存在则更新,不存在则插入)
原文:.net使用SqlBulkCopy导入数据(创建新表) .net2.0后ado.net提供了一个快速导入sqlserver的方法sqlbulkcopy.导入效率非常高. 包装了一个简单的sql ...
- MySQL临时表创建及旧表建新表
1.创建临时表 临时表是一张表,用来临时保存一些数据 特点: 只对创建该临时表的用户可见: 当会话结束时,MySQL自动删除临时表. 临时表的核心:建表和删表消耗资源极其少 创建临时表的基本格式: C ...
- mysql5.7 创建新表时提示时间戳非法
# 背景 mysql版本5.7.8,需要创建新表,研发提供的sql文件,执行后报错如下: ERROR (): Invalid default value for 'deleted_at' 就猜测到时因 ...
- 数据库SQL Server2012笔记(四)——多表查询、子查询、分页查询、用查询结果创建新表和外连接
1.多表查询 1)笛卡尔集: select * from 表名1,表名2 select * from 表名1.表名2 where 表名1.字段名=表名2.字段名 注: 若有两张表有同 ...
- mysql ---复制表结构---创建新表
1.复制表结构及数据到新表CREATE TABLE 新表SELECT * FROM 旧表这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来删 ...
- 创建新表,自动授权trigger
需求 一个用户下三个表,开发人员不定时进行rename表名称,create原表名称 as old_table 插入少量数据,另一个业务用户需要访问该表,由于表名称rename导致经常需要手工授权. 需 ...
- MySQL 复制已存在的表生成新表
从已有的表创建一个新的空表 CREATE TABLE new_table LIKE old_table; 注意: create table ... like 创建的表会保留原有表的字段.索引的定义,但 ...
随机推荐
- java基础复习二——面向对象一
面向对象三大特性:封装,继承,多态 类:对象的蓝图,生成对象的模板,是对一类事物的描述,是抽象的概念上的定义 对象:是实际存在的该类事物的每个个体,也称为实例 类之间三种关系:依赖关系(uses-a) ...
- [oracle] oracle权限传递
三个用户:SYS.lisi.wangwu ① 系统权限的传递 lisi的初始化系统权限 SQL> select * from user_sys_privs; USERNAME PRIVILEGE ...
- C语言中带参数的宏
带参数的宏定义有如下的格式: [#define 指令----带参数的宏] #define 标识符(x1,x2,……,xn) 其中 x1,x2,……xn是标志符(宏的参数) 注意:在宏的名字和括号之间 ...
- MySQL巧用sum,case...when...优化统计查询
最近在做项目,涉及到开发统计报表相关的任务,由于数据量相对较多,之前写的查询语句查询五十万条数据大概需要十秒左右的样子,后来经过老大的指点利用sum,case...when...重写SQL性能一下子提 ...
- C# 如何使用NPOI操作Excel以及读取合并单元格等
C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...
- 时间操作(struct tm、time_t)求指定日期 前n天的日期
1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值 ...
- e554. 在浏览器状态栏中显示信息
// See also e551 精简的Applet applet.showStatus("Your Message Here"); Related Examples
- bootstrap -- css -- 辅助类
文本 文本颜色 如果文本是个链接,则鼠标移动到链接文本上的时候,文本会变暗 .text-muted:灰色 .text-primary:浅蓝色 .text-success:绿色 .text-info:深 ...
- pyqt二进制和图片的转换
参考:http://blog.chinaunix.net/uid-28194872-id-3516936.html MySQL数据库要想插入图片,其字段需要是BLOB类型.BLOB (binary l ...
- C#控件方法及属性大全,望补充
C#控件及常用设计整理 1.窗体 常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体. (2) WindowState属性: 用来获取或设置窗体的窗口状态 ...