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 创建的表会保留原有表的字段.索引的定义,但 ...
随机推荐
- e681. 基本的打印程序
Note that (0, 0) of the Graphics object is at the top-left of the actual page, outside the printable ...
- C++ 中的空格
C++ 中的空格只包含空格的行,被称为空白行,可能带有注释,C++ 编译器会完全忽略它. 在 C++ 中,空格用于描述空白符.制表符.换行符和注释.空格分隔语句的各个部分,让编译器能识别语句中的某个元 ...
- 用C语言实现解析简单配置文件的小工具
本文介绍作者写的一个小工具,简单的代码中包含了C语言对字符串的处理技巧,对文本文件的简单解析,二进制文件的数据复制的方法,以及格式化输出文本文件的示例. 工具的输入是如下内容的配置文件: ;资源管理器 ...
- bootstrap -- meta中的viewport指令
在查看bootstrap教程中,碰到 <meta name="viewport" content="width=device-width, initial-scal ...
- css -- outline轮廓
outline:#00ff00 solid thick; 边框参数: 样式: none:默认,无轮廓 dotted:点状轮廓 dashed:虚线轮廓 solid:实现轮廓 double:双线轮廓,宽度 ...
- 【转】Linux内核源码分析方法
一.内核源码之我见 Linux内核代码的庞大令不少人“望而生畏”,也正因为如此,使得人们对Linux的了解仅处于泛泛的层次.如果想透析Linux,深入操作系统的本质,阅读内核源码是最有效的途径.我们都 ...
- 学习:erlang的不定长数据包头部。
- 实操演练!MathType几个绝妙小技巧!
在论文中编写公式时MathType绝对是很多人不二的选择,它的功能比较完善,操作比较方便,包含的符号模板很多,易学易上手,这些都是它的优点.但是在使用MathType时,还有很多绝妙的小技巧,使用起来 ...
- 九度 1500:出操队形(LIS变形)
题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾.突然,有一天出操负责人想了一个 ...
- Exception in thread "main" java.lang.NoSuchMethodError: org.testng.TestNG.configure(Lorg/testng/CommandLineArgs;)V
TestNG运行时报以下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.testng.TestNG. ...