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 创建的表会保留原有表的字段.索引的定义,但 ...
随机推荐
- [mysql] MySQL Order By Rand()效率【转载】
最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...
- 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 ...
- (转)RGB接口和i80接口的区别
在嵌入式的主流 LCD屏中主要支持两大类的硬件接口,一种是常见的RGB接口,另外一种是MCU接口.后面因为最早是针对单片机的领域在使用,因此得名.后在中低端手机大量使用,其主要特点是价格便宜的. M ...
- 10个最佳的触控手式的JavaScript框架(转)
由于各种原因移动开发是一项艰难的工作,比如它是非常耗时的.充满压力的任务.最重要的是,作为一个开发人员,你必须保持更新所有最新 的技术和技巧——你必须知道所有最新的趋势,问题和解决方案等.例如跨浏览器 ...
- jquery widgets 弹框
<div id='dialog' style="display:none;"> <div style="text-align:center;" ...
- asp.net mvc清空指定cookies
HttpCookie hc = Request.Cookies["user"]; hc.Expires = DateTime.Now.AddDays( ...
- ssm框架搭建并演示获取json字符串
为方便起见,使用oracle自带的scott账户中的emp表作为数据源. 预期效果: 1.输入URL:http://localhost:8888/ssm/getEmp?empno=7369 2.返回结 ...
- solr 5.2.1 tomcat 7 配置过程笔记
因为这个是新版,网上很少这个配置文档,看网上其他的教程弄了很多次,都没有成功,幸亏有这个链接的文档, 才迅速的配置成功,其实是比以前简洁了.因为我的在 linux 上面安装,不方便截图,直接复制修改了 ...
- SharePoint 2013 设置customErrors显示实际的错误信息
一.首先设置IIS中的Web.config文件 找到对应的IIS应用程序目录,如:C:\inetpub\wwwroot\wss\VirtualDirectories\3000 在此文件夹下包含一个we ...
- 如何查看linux命令源代码
如何查看linux命令源代码 用linux一段时间了,有时候想看看ls.cat.more等命令的源代码,在下载的内核源码中用cscope没能找到,在网上搜索了一下,将方 法总结如下: 以搜索ls命令源 ...