当前位置:网站首页>PostgreSQL中的表复制
PostgreSQL中的表复制
2022-07-07 09:03:00 【秋漓】
PostgreSQL提供了两种方式用来进行表复制:
1、create table as
2、create table like
数据准备
创建含有序列、主键、注释、CHECK约束的tmp表用于后续测试:
create table tmp(
id SERIAL,
name VARCHAR(100) primary key,
num int CHECK(10<num and num<100)
);
comment on column tmp.id is '标识码';
comment on column tmp.name is '名称';
comment on column tmp.num is '数量';
insert into tmp(name,num) VALUES('苹果',61);
insert into tmp(name,num) VALUES('香蕉',51);
表的DDL展示:
下面就来讲讲两种表复制方式的结果:
create table as
挺常规的方法,在复制表结构的时候还可以带上数据。不过这种复制会丢失原有表的注释、约束等信息。
create table tmp_x as select * from tmp where name='苹果';
DDL:
create table like
语法:create table tar_table LIKE source_table [ like_option ... ]
like_option有下列几种选择:
1、INCLUDING COMMENTS:注释
2、INCLUDING CONSTRAINTS :CHECK约束
3、INCLUDING DEFAULTS:被拷贝的列定义的默认表达式才会被拷贝。默认的行为是排除默认表达式,导致新表中被拷贝过来的列的默认值为空值。注意,如果拷贝的默认值调用了数据库修改函数(如nextval),则可能在原始表和新表之间创建功能联系。
4、INCLUDING IDENTITY:拷贝复制字段定义的标识声明。 为新表的每个标识列创建一个新的序列,与旧表相关的序列区分开。
5、INCLUDING INDEXES :主键约束 和索引约束
6、INCLUDING STORAGE:复制而来的列定义的STORAGE设置才会被复制。默认行为会排除STORAGE设置,导致新表中复制而来的列具有与类型相关的默认设置
7、INCLUDING STATISTICS:扩展统计会被复制到新表
8、INCLUDING ALL:是 INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING INDEXES INCLUDING STATISTICS INCLUDING STORAGE.的简写形式。
一般我们复制时只用 INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING INDEXES就够了。
示例1:
create table tmp_a (like tmp);
示例2:
只使用INCLUDING DEFAULTS拷贝列定义
create table tmp_b (like tmp INCLUDING defaults);
示例3:
使用INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING INDEXES拷贝注释、约束
create table tmp_c (like tmp INCLUDING comments including constraints including indexes);
各种拷贝结果的DDL:
![]() 示例1 | ![]() 示例2 | ![]() 示例3 |
春江潮水连海平,海上明月共潮生。 --张若虚《春江花月夜》
边栏推荐
- Basic knowledge of process (orphan, zombie process)
- mif 文件格式记录
- Is the gold content of intermediate e-commerce division in the soft exam high?
- seata 1.3.0 四种模式解决分布式事务(AT、TCC、SAGA、XA)
- [système recommandé 01] rechub
- 想考中级软考,一般需要多少复习时间?
- 请问申购新股哪个证券公司开户是最好最安全的
- 2021-04-23
- Is the soft test intermediate useful??
- 软考信息处理技术员有哪些备考资料与方法?
猜你喜欢

“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题

SQL Server knowledge gathering 9: modifying data

Transaction rolled back because it has been marked as rollback only

Monai version has been updated to 0.9. See what new functions it has

Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)
![P1223 queuing for water /1319: [example 6.1] queuing for water](/img/09/29f19b32f0a3c82ddb3c30d5d0b16e.png)
P1223 queuing for water /1319: [example 6.1] queuing for water
![[C #] the solution of WinForm operation zoom (blur)](/img/66/b70213c95acd4ad9fe2eb739bd46b5.png)
[C #] the solution of WinForm operation zoom (blur)

2021 summary and 2022 outlook
![[pyqt] the cellwidget in tablewidget uses signal and slot mechanism](/img/0e/02265f7195ca0add4155694530822a.png)
[pyqt] the cellwidget in tablewidget uses signal and slot mechanism

July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
随机推荐
一些线上学术报告网站与机器学习视频
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
2021-04-23
The fifth training assignment
Get pictures through opencv, change channels and save them
2022.7.5DAY597
【OneNote】无法连接到网络,无法同步问题
IDEA快捷键大全
简单易修改的弹框组件
【亲测可行】error while loading shared libraries的解决方案
中级网络工程师是什么?主要是考什么,有什么用?
How to prepare for the advanced soft test (network planning designer)?
The eighth training assignment
Simple and easy to modify spring frame components
The difference between monotonicity constraint and anti monotonicity constraint
变量的解构赋值
[C #] the solution of WinForm operation zoom (blur)
[untitled]
Unity script visualization about layout code
2021-05-21


