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 创建的表会保留原有表的字段.索引的定义,但 ...
随机推荐
- 如何退出Activity?如何安全退出已调用多个Activity的Application?
对于单一Activity的应用来说,退出很简单,直接finish()即可. 1.抛异常强制退出: 该方法通过抛异常,使程序ForceClose. 验证可以,但是,需要解决的问题是,如何使程序结束掉,而 ...
- 【转】WCF入门教程六[一个简单的Demo]
一.前言 前面的几个章节介绍了很多理论基础,如:什么是WCF.WCF中的A.B.C.WCF的传输模式.本文从零开始和大家一起写一个小的WCF应用程序Demo. 大多框架的学习都是从增.删.改.查开始来 ...
- C# 发邮件 服务器响应为: 5.7.0 Must issue a STARTTLS command first
The SMTP server requires a secure connection or the client was not authenticated. The server respons ...
- 创建以API为中心的Web应用(转)
英文原文:Creating an API-Centric Web Application 引言 API——API是Application Programming Interface(应用编程接口)的简 ...
- C Language Study - 函数指针的使用
函数指针的使用 1.函数指针定义 第一次使用函数指针,我是这样声明的,typedef void ( *pSimulatepks )( uint8 *prxBuf, uint8 *prxBufLen ) ...
- hdu2147 kiki's game(博弈)
这个是纳什博弈?不知道怎么看的 依据PN图,从左下角開始推 左下角P 最后一行都是PNPNPN 第一列都是 P N P N P 完了填完即可了 #include<cstdio> int m ...
- 如何研究某个gene的ceRNA 网络
研究人员针对 PTEN 这个关键的抑癌基因,来探究调控该基因表达的ceRNA 网络: 分析策略: 1)预测能调控该基因的miRNAs 通过miRanda 软件预测和实验验证相结合的方式,挑选出 miR ...
- Json.net 时间格式处理
json.net转json时生成的时间格式是这种 2015-11-14T06:59:59+08:00 格式化为这种2015-11-14 后台代码: IsoDateTimeConverter timeF ...
- PHP+tcpdf的生成
项目中,可能用到额 查看:http://www.sucaihuo.com/php/217.html
- openal 基础知识
原文地址:http://blog.csdn.net/woker/article/details/8687380 一简介 OpenAL抽象出三种基本对象:buffers(缓冲区).sources(源). ...