当前位置:网站首页>Table replication in PostgreSQL
Table replication in PostgreSQL
2022-07-07 11:14:00 【Autumn Li】
PostgreSQL Two methods are provided for table replication :
1、create table as
2、create table like
Data preparation
Create a sequence 、 Primary key 、 notes 、CHECK Constrained tmp The table is used for subsequent tests :
create table tmp(
id SERIAL,
name VARCHAR(100) primary key,
num int CHECK(10<num and num<100)
);
comment on column tmp.id is ' Identification code ';
comment on column tmp.name is ' name ';
comment on column tmp.num is ' Number ';
insert into tmp(name,num) VALUES(' Apple ',61);
insert into tmp(name,num) VALUES(' Banana ',51);
Tabular DDL Exhibition :
Let's talk about the results of the two table replication methods :
create table as
Quite conventional method , When copying the table structure, you can also bring data . However, this copy will lose the comments of the original table 、 Constraints and other information .
create table tmp_x as select * from tmp where name=' Apple ';
DDL:
create table like
grammar :create table tar_table LIKE source_table [ like_option ... ]
like_option There are several options :
1、INCLUDING COMMENTS: notes
2、INCLUDING CONSTRAINTS :CHECK constraint
3、INCLUDING DEFAULTS: The default expression defined by the copied column will be copied . The default behavior is to exclude the default expression , The default value of the copied column in the new table is null . Be careful , If the default value of the copy calls the database modification function ( Such as nextval), It is possible to create a functional connection between the original table and the new table .
4、INCLUDING IDENTITY: Copy the identification statement defined by the copy field . Create a new sequence for each identity column of the new table , The sequence related to the old table is distinguished .
5、INCLUDING INDEXES : Primary key constraint And index constraints
6、INCLUDING STORAGE: The copied column defines STORAGE Settings will be copied . The default behavior excludes STORAGE Set up , This causes the copied columns in the new table to have type related default settings
7、INCLUDING STATISTICS: The extended statistics will be copied to the new table
8、INCLUDING ALL: yes INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING INDEXES INCLUDING STATISTICS INCLUDING STORAGE. Short form of .
Generally, we only use INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING INDEXES That's enough .
Example 1:
create table tmp_a (like tmp);
Example 2:
Use only INCLUDING DEFAULTS Copy column definitions
create table tmp_b (like tmp INCLUDING defaults);
Example 3:
Use INCLUDING COMMENTS INCLUDING CONSTRAINTS INCLUDING INDEXES Copy notes 、 constraint
create table tmp_c (like tmp INCLUDING comments including constraints including indexes);
Of various copy results DDL:
![]() Example 1 | ![]() Example 2 | ![]() Example 3 |
The spring tide is even with the sea , The moon and the tide rise in the sea . -- Zhang Ruoxu 《 The spring river and the moon 》
边栏推荐
- Web端自动化测试失败的原因
- Go Slice 比较
- Add a self incrementing sequence number to the antd table component
- Basic knowledge of process (orphan, zombie process)
- Arduino board description
- 关于测试人生的一站式发展建议
- [OneNote] can't connect to the network and can't sync the problem
- SQL Server knowledge collection 11: Constraints
- 如何在博客中添加Aplayer音乐播放器
- 数据库同步工具 DBSync 新增对MongoDB、ES的支持
猜你喜欢
![[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk](/img/41/3a9450a84291ba04caee65241bce5d.png)
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk

Force buckle 1002 Find common characters

What if copying is prohibited?

Wallhaven wallpaper desktop version
![[pyqt] the cellwidget in tablewidget uses signal and slot mechanism](/img/0e/02265f7195ca0add4155694530822a.png)
[pyqt] the cellwidget in tablewidget uses signal and slot mechanism

Unity script generates configurable files and loads

Idea shortcut keys

Socket socket programming

Ping tool ICMP message learning

Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)
随机推荐
IDEA快捷键大全
技术分享 | 抓包分析 TCP 协议
Socket socket programming
Qt|多个窗口共有一个提示框类
请问申购新股哪个证券公司开户是最好最安全的
在我有限的软件测试经历里,一段专职的自动化测试经验总结
Use of dotween
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
Simple and easy to modify spring frame components
[pytorch 07] hands on deep learning chapter_ Preliminaries/ndarray exercises hands-on version
Opencv installation and environment configuration - vs2017
2021 summary and 2022 outlook
[C #] the solution of WinForm operation zoom (blur)
"Dream Cup" 2017 Jiangsu information and future primary school summer camp it expert PK program design questions
The sixth training assignment
Unity script visualization about layout code
Input type= "password" how to solve the problem of password automatically brought in
seata 1.3.0 四种模式解决分布式事务(AT、TCC、SAGA、XA)
TDengine 社区问题双周精选 | 第二期
[untitled]


