↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1 / 7.0 / 6.5 / 6.4
历史版本。 PostgreSQL 6.4 已结束支持。 请参阅 当前版本手册.

INSERT

INSERT — 向一个表中插入新行

大纲

INSERT INTO table [ ( column [, ...] ) ]
    { VALUES ( expression [, ...] ) | SELECT query }
  

输入

table

一个已存在表的名称。

column

table中一个列的名称。

expression

要赋予column的一个有效表达式或值。

query

一个有效的查询。有效参数的进一步描述请参考 SELECT 语句。

输出

→ INSERT oid 1

只插入了一行时返回的消息。→ oid 是被插入行的行标识符。

→ INSERT 0 #

插入了多行时返回的消息。→ # 是插入的行数。

描述

INSERT 允许我们向一个表中插入新行。可以一次插入一行,也可以插入一个查询结果的多个行。目标列表中的列可以按任意顺序列出。对于目标列表中没有出现的每个列,将插入默认值;如果列没有声明的默认值,则假定为 NULL。如果每个列的表达式数据类型不正确,将尝试自动类型强制转换。

要向一个表追加数据,你必须拥有该表上的 insert 权限,并且对 WHERE 子句中指定的任何表拥有 select 权限。

用法

--Insert a single row into table films;
--(in the second example the column date_prod is omitted 
--therefore will be stored in it a default value of NULL):
--
INSERT INTO films VALUES
    ('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute');

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama');

--Insert a single row into table distributors, note that
--only column "name" is specified, to the non specified
--column "did" will be assigned its default value:
--
INSERT INTO distributors (name) VALUES ('British Lion');

--Insert several rows into table films from table tmp:
--
INSERT INTO films
    SELECT * FROM tmp;

--Insert into arrays:
--Create an empty 3x3 gameboard for noughts-and-crosses
--(all of these queries create the same board attribute)
--(Refer to the PostgreSQL User's Guide for further
--information about arrays).

INSERT INTO tictactoe (game, board[1:3][1:3])
    VALUES (1,'{{"","",""},{},{"",""}}');
INSERT INTO tictactoe (game, board[3][3])
    VALUES (2,'{}');
INSERT INTO tictactoe (game, board)
    VALUES (3,'{{,,},{,,},{,,}}');

兼容性

SQL92

INSERT 语句与 SQL92 完全兼容。query子句特性方面可能存在的限制记录在 SELECT 语句中。

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.