久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么使用PostgreSQL中的COPY命令

共計(jì) 3434 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。

這篇文章主要講解了“怎么使用 PostgreSQL 中的 COPY 命令”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著丸趣 TV 小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么使用 PostgreSQL 中的 COPY 命令”吧!

Copy 命令在 PG 12 有所增強(qiáng), 在 COPY FROM 時(shí)可添加 WHERE 條件過(guò)濾.

PG 11
Copy 命令

testdb=# \help copy
Command: COPY
Description: copy data between a file and a table
Syntax:
COPY table_name [ ( column_name [, ...] ) ]
 FROM {  filename  | PROGRAM  command  | STDIN }
 [ [ WITH ] ( option [, ...] ) ]
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
 TO {  filename  | PROGRAM  command  | STDOUT }
 [ [ WITH ] ( option [, ...] ) ]
where option can be one of:
 FORMAT format_name
 OIDS [ boolean ]
 FREEZE [ boolean ]
 DELIMITER  delimiter_character 
 NULL  null_string 
 HEADER [ boolean ]
 QUOTE  quote_character 
 ESCAPE  escape_character 
 FORCE_QUOTE { ( column_name [, ...] ) | * }
 FORCE_NOT_NULL ( column_name [, ...] )
 FORCE_NULL ( column_name [, ...] )
 ENCODING  encoding_name

簡(jiǎn)單使用

testdb=# drop table if exists t_copy;
DROP TABLE
testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));
CREATE TABLE
testdb=# insert into t_copy SELECT x, c1- ||x FROM generate_series(1, 1000) AS x;
INSERT 0 1000
testdb=# 
testdb=# COPY t_copy TO  /tmp/data/t_copy.txt  with DELIMITER  | 
COPY 1000
testdb=# drop table if exists t_import;
DROP TABLE
testdb=# CREATE TABLE t_import(id int,c1 varchar(20));
CREATE TABLE
testdb=# COPY t_import FROM  /tmp/data/t_copy.txt  with DELIMITER  | 
COPY 1000
testdb=# select * from t_import limit 10;
 id | c1 
----+-------
 1 | c1-1
 2 | c1-2
 3 | c1-3
 4 | c1-4
 5 | c1-5
 6 | c1-6
 7 | c1-7
 8 | c1-8
 9 | c1-9
 10 | c1-10
(10 rows)

不支持 WHERE 條件過(guò)濾

testdb=# COPY t_import FROM  /tmp/data/t_copy.txt  with DELIMITER  |  where id   5;
ERROR: syntax error at or near  where 
LINE 1: ...t FROM  /tmp/data/t_copy.txt  with DELIMITER  |  where id  ...

PG 12
COPY 命令語(yǔ)法

[local]:5432 pg12@testdb=# \help copy
Command: COPY
Description: copy data between a file and a table
Syntax:
COPY table_name [ ( column_name [, ...] ) ]
 FROM {  filename  | PROGRAM  command  | STDIN }
 [ [ WITH ] ( option [, ...] ) ]
 [ WHERE condition ]
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
 TO {  filename  | PROGRAM  command  | STDOUT }
 [ [ WITH ] ( option [, ...] ) ]
where option can be one of:
 FORMAT format_name
 FREEZE [ boolean ]
 DELIMITER  delimiter_character 
 NULL  null_string 
 HEADER [ boolean ]
 QUOTE  quote_character 
 ESCAPE  escape_character 
 FORCE_QUOTE { ( column_name [, ...] ) | * }
 FORCE_NOT_NULL ( column_name [, ...] )
 FORCE_NULL ( column_name [, ...] )
 ENCODING  encoding_name 
URL: https://www.postgresql.org/docs/12/sql-copy.html

支持 WHERE 條件過(guò)濾

[local]:5432 pg12@testdb=# drop table if exists t_copy;
DROP TABLE
Time: 50.327 ms
[local]:5432 pg12@testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));
CREATE TABLE
Time: 5.038 ms
[local]:5432 pg12@testdb=# insert into t_copy SELECT x, c1- ||x FROM generate_series(1, 1000) AS x;
INSERT 0 1000
Time: 16.422 ms
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# COPY t_copy TO  /tmp/data/t_copy.txt  with DELIMITER  | 
COPY 1000
Time: 4.795 ms
[local]:5432 pg12@testdb=# drop table if exists t_import;
DROP TABLE
Time: 4.798 ms
[local]:5432 pg12@testdb=# CREATE TABLE t_import(id int,c1 varchar(20));
CREATE TABLE
Time: 2.462 ms
[local]:5432 pg12@testdb=# COPY t_import FROM  /tmp/data/t_copy.txt  with DELIMITER  |  WHERE id   5;
COPY 4
Time: 4.842 ms
[local]:5432 pg12@testdb=# select * from t_import;
 id | c1 
----+------
 1 | c1-1
 2 | c1-2
 3 | c1-3
 4 | c1-4
(4 rows)
Time: 6.103 ms

感謝各位的閱讀,以上就是“怎么使用 PostgreSQL 中的 COPY 命令”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么使用 PostgreSQL 中的 COPY 命令這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-26發(fā)表,共計(jì)3434字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 宾阳县| 司法| 金华市| 临清市| 巴林左旗| 合川市| 甘孜县| 镶黄旗| 嘉善县| 杭锦后旗| 金塔县| 平原县| 西藏| 石棉县| 石嘴山市| 蒲江县| 梨树县| 浦县| 宁武县| 云安县| 克山县| 儋州市| 新竹县| 托克托县| 平阳县| 枣庄市| 乌兰县| 黎城县| 全椒县| 沾化县| 苏尼特左旗| 根河市| 马尔康县| 道真| 子洲县| 淅川县| 东台市| 新源县| 桂平市| 白河县| 衡东县|