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 创建的表会保留原有表的字段.索引的定义,但 ...
随机推荐
- thinkPHP的优缺点
适合大量重复的工作,但不太灵活...
- VS2008兼容安装
1. 直接安装出现问题:该项目的所有配置都需要系统提供对某些 平台的支持,但在此计算机上没有安装这些平台.因此无法加载该项目. 解决方法:先卸载原来安装的, 再设置安装包中的setup.exe的兼容性 ...
- PHP与ASP.NET的优劣比较
PHP与ASP.NET的比较 表 1 PHP 4 PHP5 ASP.NET 软件价格 免费 免费 免费 平台价格 免费 免费 $$ 速度 强 强 弱 效率 强 强 弱 安全性 强 强 强 平台 强 强 ...
- 使用avahi 的mdns服务发现server
avahi-browse -a 可以查看局域网内所有的mdns服务, avahi-browse -r _xxxxx._tcp
- endl的读法
endl是“end line”的缩写,所以它应该念作“endELL”而不是“endONE”.
- 如果返回结构体类型变量(named return value optimisation,NRVO)
貌似这是一个非常愚蠢的问题,因为对于具有良好素质的程序员而言,在C中函数返回类型为结构体类型是不是有点不合格,干嘛不用指针做传入传出呢? 测试环境:Linux IOS 3.2.0-45-generic ...
- 验证码显示不出来,在THINKPHP中的使用
未开启 php_gd2设置 php的配置文件php.ini,搜索extension=php_gd2.dll,去掉前面的分号即可: 1.在模块类中增加一个 verify 方法来用于显示验证码Public ...
- mysql数据库,查看数据存放目录datadir
需求描述: 在使用数据库,或者刚接手一个数据库时,可以查看该数据库的数据文件存放在什么位置. 操作过程: 1.通过查看datadir系统变量来查看数据目录 [mysql@redhat6 mysql-b ...
- golang包管理
https://studygolang.com/articles/8413 https://studygolang.com/articles/10523
- hadoop程序MapReduce之WordCount
需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...