Postgresql 中文操作指南
Synopsis
[ WITH [ RECURSIVE ] with_query [, ...] ]
INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ ON CONFLICT [ conflict_target ] conflict_action ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
where conflict_target can be one of:
( { index_column_name | ( index_expression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE index_predicate ]
ON CONSTRAINT constraint_name
and conflict_action is one of:
DO NOTHING
DO UPDATE SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ WHERE condition ]
Description
INSERT 在表中插入新行。可以通过值表达式指定插入一行或多行,或从查询产生的零行或多行。
INSERT inserts new rows into a table. One can insert one or more rows specified by value expressions, or zero or more rows resulting from a query.
目标列名称可以以任何顺序列出。如果完全不指定列名称的列表,则默认值为表的所有列按照其声明的顺序;或 N 列名称,如果仅通过 VALUES 子句或 query 提供 N 个列。由 VALUES 子句或 query 提供的值与显式或隐式列列表从左到右关联。
The target column names can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order; or the first N column names, if there are only N columns supplied by the VALUES clause or query. The values supplied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right.
在显式或隐式列列表中不出现的每一列都将填充一个默认值,即其声明的默认值,如果没有默认值,则填充 null。
Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none.
如果任何列的表达式不是正确的数据类型,则会尝试自动类型转换。
If the expression for any column is not of the correct data type, automatic type conversion will be attempted.
INSERT 的表中没有唯一索引不会被并发活动阻塞。如果并发会话执行锁定或修改与正在插入的唯一索引值匹配的行,则具有唯一索引的表可能会被阻塞;详细的信息请参阅 Section 64.5 。可以 ON CONFLICT 用于指定替代动作以发出唯一约束或排除约束违规错误。(参见下面的 ON CONFLICT Clause 。)
INSERT into tables that lack unique indexes will not be blocked by concurrent activity. Tables with unique indexes might block if concurrent sessions perform actions that lock or modify rows matching the unique index values being inserted; the details are covered in Section 64.5. ON CONFLICT can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error. (See ON CONFLICT Clause below.)
可选 RETURNING 子句导致 INSERT 计算并返回基于实际上插入的每一行的值(或更新,如果使用了 ON CONFLICT DO UPDATE 子句)。这主要用于获取由默认值提供的数值,如序列序列号。但是,允许使用表的列的任何表达式。 RETURNING 列表的语法与 SELECT 的输出列表相同。只返回成功插入或更新的行。例如,如果行被锁定但未更新,因为 ON CONFLICT DO UPDATE … WHERE 子句 condition 不满足,则不会返回该行。
The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted (or updated, if an ON CONFLICT DO UPDATE clause was used). This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table’s columns is allowed. The syntax of the RETURNING list is identical to that of the output list of SELECT. Only rows that were successfully inserted or updated will be returned. For example, if a row was locked but not updated because an ON CONFLICT DO UPDATE … WHERE clause condition was not satisfied, the row will not be returned.
您必须对表具有 INSERT 权限才能向其中进行插入。如果 ON CONFLICT DO UPDATE 存在,则还需要对表有 UPDATE 权限。
You must have INSERT privilege on a table in order to insert into it. If ON CONFLICT DO UPDATE is present, UPDATE privilege on the table is also required.
如果指定了列列表,则只需要对列出的列有 INSERT 权限。类似地,当指定 ON CONFLICT DO UPDATE 时,您只需要对列出用于更新的列(或列)有 UPDATE 权限。但是, ON CONFLICT DO UPDATE 还需要对 ON CONFLICT DO UPDATE 表达式或 condition 中读取其值的任何列有 SELECT 权限。
If a column list is specified, you only need INSERT privilege on the listed columns. Similarly, when ON CONFLICT DO UPDATE is specified, you only need UPDATE privilege on the column(s) that are listed to be updated. However, ON CONFLICT DO UPDATE also requires SELECT privilege on any column whose values are read in the ON CONFLICT DO UPDATE expressions or condition.
使用 RETURNING 子句要求对 RETURNING 中提到的所有列有 SELECT 权限。如果您使用 query 子句从查询中插入行,那么您当然需要对查询中使用的任何表或列有 SELECT 权限。
Use of the RETURNING clause requires SELECT privilege on all columns mentioned in RETURNING. If you use the query clause to insert rows from a query, you of course need to have SELECT privilege on any table or column used in the query.
Parameters
Inserting
本节涵盖只在插入新行时可以使用的参数。与 ON CONFLICT 子句使用 exclusively 参数被单独描述。
This section covers parameters that may be used when only inserting new rows. Parameters exclusively used with the ON CONFLICT clause are described separately.
-
with_query
-
The WITH clause allows you to specify one or more subqueries that can be referenced by name in the INSERT query. See Section 7.8 and SELECT for details.
-
It is possible for the query (SELECT statement) to also contain a WITH clause. In such a case both sets of with_query can be referenced within the query, but the second one takes precedence since it is more closely nested.
-
-
table_name
-
The name (optionally schema-qualified) of an existing table.
-
-
alias
-
A substitute name for table_name. When an alias is provided, it completely hides the actual name of the table. This is particularly useful when ON CONFLICT DO UPDATE targets a table named excluded, since that will otherwise be taken as the name of the special table representing the row proposed for insertion.
-
-
column_name
-
The name of a column in the table named by table_name. The column name can be qualified with a subfield name or array subscript, if needed. (Inserting into only some fields of a composite column leaves the other fields null.) When referencing a column with ON CONFLICT DO UPDATE, do not include the table’s name in the specification of a target column. For example, INSERT INTO table_name … ON CONFLICT DO UPDATE SET table_name.col = 1 is invalid (this follows the general behavior for UPDATE).
-
-
OVERRIDING SYSTEM VALUE
-
If this clause is specified, then any values supplied for identity columns will override the default sequence-generated values.
-
For an identity column defined as GENERATED ALWAYS, it is an error to insert an explicit value (other than DEFAULT) without specifying either OVERRIDING SYSTEM VALUE or OVERRIDING USER VALUE. (For an identity column defined as GENERATED BY DEFAULT, OVERRIDING SYSTEM VALUE is the normal behavior and specifying it does nothing, but PostgreSQL allows it as an extension.)
-
-
OVERRIDING USER VALUE
-
If this clause is specified, then any values supplied for identity columns are ignored and the default sequence-generated values are applied.
-
This clause is useful for example when copying values between tables. Writing INSERT INTO tbl2 OVERRIDING USER VALUE SELECT * FROM tbl1 will copy from tbl1 all columns that are not identity columns in tbl2 while values for the identity columns in tbl2 will be generated by the sequences associated with tbl2.
-
-
DEFAULT VALUES
-
All columns will be filled with their default values, as if DEFAULT were explicitly specified for each column. (An OVERRIDING clause is not permitted in this form.)
-
-
expression
-
An expression or value to assign to the corresponding column.
-
-
DEFAULT
-
The corresponding column will be filled with its default value. An identity column will be filled with a new value generated by the associated sequence. For a generated column, specifying this is permitted but merely specifies the normal behavior of computing the column from its generation expression.
-
-
query
-
A query (SELECT statement) that supplies the rows to be inserted. Refer to the SELECT statement for a description of the syntax.
-
-
output_expression
-
An expression to be computed and returned by the INSERT command after each row is inserted or updated. The expression can use any column names of the table named by table_name. Write * to return all columns of the inserted or updated row(s).
-
-
output_name
-
A name to use for a returned column.
-
ON CONFLICT Clause
可选的 ON CONFLICT 子句指定了一个不同的操作来引发唯一违规或排除约束违规错误。对于提议插入的每一行,要么进行插入,要么如果违反了 conflict_target 指定的 arbiter 约束或索引,则执行不同的 conflict_action 。 ON CONFLICT DO NOTHING 只会避免将其作为替代操作插入一行。 ON CONFLICT DO UPDATE 将更新与提议插入的行冲突的现有行为作为其替代操作。
The optional ON CONFLICT clause specifies an alternative action to raising a unique violation or exclusion constraint violation error. For each individual row proposed for insertion, either the insertion proceeds, or, if an arbiter constraint or index specified by conflict_target is violated, the alternative conflict_action is taken. ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action. ON CONFLICT DO UPDATE updates the existing row that conflicts with the row proposed for insertion as its alternative action.
conflict_target 可以执行 unique index inference 。在执行推理时,它包含一个或多个 index_column_name 列和/或 index_expression 表达式,以及一个可选的 index_predicate 。在不考虑顺序的情况下,所有恰好包含 conflict_target 指定的列/表达式的 table_name 唯一索引都被推断(选择)为仲裁索引。如果指定了 index_predicate ,则它必须进一步满足仲裁索引的推理要求。请注意,这意味着如果存在满足所有其他条件的此类索引,则将推断(因此 ON CONFLICT 使用)非部分唯一索引(没有谓词的唯一索引)。如果推理尝试失败,则会引发错误。
conflict_target can perform unique index inference. When performing inference, it consists of one or more index_column_name columns and/or index_expression expressions, and an optional index_predicate. All table_name unique indexes that, without regard to order, contain exactly the conflict_target-specified columns/expressions are inferred (chosen) as arbiter indexes. If an index_predicate is specified, it must, as a further requirement for inference, satisfy arbiter indexes. Note that this means a non-partial unique index (a unique index without a predicate) will be inferred (and thus used by ON CONFLICT) if such an index satisfying every other criteria is available. If an attempt at inference is unsuccessful, an error is raised.
ON CONFLICT DO UPDATE 保证了原子 INSERT 或 UPDATE 结果;只要没有独立的错误,这两个结果之一就能得到保证,即使在高并发的情况下也是如此。这也称为 UPSERT ——“更新或插入”。
ON CONFLICT DO UPDATE guarantees an atomic INSERT or UPDATE outcome; provided there is no independent error, one of those two outcomes is guaranteed, even under high concurrency. This is also known as UPSERT — “UPDATE or INSERT”.
-
conflict_target
-
Specifies which conflicts ON CONFLICT takes the alternative action on by choosing arbiter indexes. Either performs unique index inference, or names a constraint explicitly. For ON CONFLICT DO NOTHING, it is optional to specify a conflict_target; when omitted, conflicts with all usable constraints (and unique indexes) are handled. For ON CONFLICT DO UPDATE, a conflict_target must be provided.
-
-
conflict_action
-
conflict_action specifies an alternative ON CONFLICT action. It can be either DO NOTHING, or a DO UPDATE clause specifying the exact details of the UPDATE action to be performed in case of a conflict. The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table’s name (or an alias), and to the row proposed for insertion using the special excluded table. SELECT privilege is required on any column in the target table where corresponding excluded columns are read.
-
Note that the effects of all per-row BEFORE INSERT triggers are reflected in excluded values, since those effects may have contributed to the row being excluded from insertion.
-
-
index_column_name
-
The name of a table_name column. Used to infer arbiter indexes. Follows CREATE INDEX format. SELECT privilege on index_column_name is required.
-
-
index_expression
-
Similar to index_column_name, but used to infer expressions on table_name columns appearing within index definitions (not simple columns). Follows CREATE INDEX format. SELECT privilege on any column appearing within index_expression is required.
-
-
collation
-
When specified, mandates that corresponding index_column_name or index_expression use a particular collation in order to be matched during inference. Typically this is omitted, as collations usually do not affect whether or not a constraint violation occurs. Follows CREATE INDEX format.
-
-
opclass
-
When specified, mandates that corresponding index_column_name or index_expression use particular operator class in order to be matched during inference. Typically this is omitted, as the equality semantics are often equivalent across a type’s operator classes anyway, or because it’s sufficient to trust that the defined unique indexes have the pertinent definition of equality. Follows CREATE INDEX format.
-
-
index_predicate
-
Used to allow inference of partial unique indexes. Any indexes that satisfy the predicate (which need not actually be partial indexes) can be inferred. Follows CREATE INDEX format. SELECT privilege on any column appearing within index_predicate is required.
-
-
constraint_name
-
Explicitly specifies an arbiter constraint by name, rather than inferring a constraint or index.
-
-
condition
-
An expression that returns a value of type boolean. Only rows for which this expression returns true will be updated, although all rows will be locked when the ON CONFLICT DO UPDATE action is taken. Note that condition is evaluated last, after a conflict has been identified as a candidate to update.
-
请注意,在使用 ON CONFLICT DO UPDATE 时,排除约束不受支持作为仲裁。在所有情况下,只有 NOT DEFERRABLE 约束和唯一索引才受支持作为仲裁。
Note that exclusion constraints are not supported as arbiters with ON CONFLICT DO UPDATE. In all cases, only NOT DEFERRABLE constraints and unique indexes are supported as arbiters.
带有 ON CONFLICT DO UPDATE 子句的 INSERT 是“确定性”语句。这意味着不允许该命令对任何单个现有行造成多次影响;在出现此情况时会引发基数冲突错误。建议插入的行为在由仲裁索引或约束约束的属性方面彼此不重复。
INSERT with an ON CONFLICT DO UPDATE clause is a “deterministic” statement. This means that the command will not be allowed to affect any single existing row more than once; a cardinality violation error will be raised when this situation arises. Rows proposed for insertion should not duplicate each other in terms of attributes constrained by an arbiter index or constraint.
请注意,目前不支持将应用于已分区表的 INSERT 的 ON CONFLICT DO UPDATE 子句更新冲突行的分区键,使其要求将该行移动到新分区。
Note that it is currently not supported for the ON CONFLICT DO UPDATE clause of an INSERT applied to a partitioned table to update the partition key of a conflicting row such that it requires the row be moved to a new partition.
Tip
通常情况下,最好使用唯一索引推断,而不是直接使用 ON CONFLICT ON CONSTRAINT constraint_name 命名约束。当基础索引被另一个或多或少相等的索引以重叠的方式替换时,推断将继续正常工作,例如在删除要替换的索引之前使用 CREATE UNIQUE INDEX … CONCURRENTLY 时。
It is often preferable to use unique index inference rather than naming a constraint directly using ON CONFLICT ON CONSTRAINT constraint_name. Inference will continue to work correctly when the underlying index is replaced by another more or less equivalent index in an overlapping way, for example when using CREATE UNIQUE INDEX … CONCURRENTLY before dropping the index being replaced.
Outputs
成功完成后, INSERT 命令将返回以下形式的命令标记
On successful completion, an INSERT command returns a command tag of the form
INSERT oid count
count 是已插入或更新的行数。 oid 始终为 0(如果 count 正好为 1 且目标表已声明为 WITH OIDS ,则它过去是分配给插入行的 OID,否则为 0,但是不支持再创建表 WITH OIDS )。
The count is the number of rows inserted or updated. oid is always 0 (it used to be the OID assigned to the inserted row if count was exactly one and the target table was declared WITH OIDS and 0 otherwise, but creating a table WITH OIDS is not supported anymore).
如果 INSERT 命令包含 RETURNING 子句,则结果将类似于 SELECT 语句的结果,该语句包含在 RETURNING 列表中定义的列和值,这些列和值时针对该命令插入或更新的行计算的。
If the INSERT command contains a RETURNING clause, the result will be similar to that of a SELECT statement containing the columns and values defined in the RETURNING list, computed over the row(s) inserted or updated by the command.
Notes
如果指定表是已分区表,则将把每行路由到适当的分区并将其插入到该分区中。如果指定表是一个分区,则当输入行之一违反分区约束时,将会发生错误。
If the specified table is a partitioned table, each row is routed to the appropriate partition and inserted into it. If the specified table is a partition, an error will occur if one of the input rows violates the partition constraint.
您也许还需要考虑使用 MERGE ,因为它允许在单个语句中组合 INSERT 、 UPDATE 和 DELETE 。请参见 MERGE 。
You may also wish to consider using MERGE, since that allows mixing INSERT, UPDATE, and DELETE within a single statement. See MERGE.
Examples
插入一行到表 films 中:
Insert a single row into table films:
INSERT INTO films VALUES
('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');
在此示例中,已省略 len 列,因此它将具有默认值:
In this example, the len column is omitted and therefore it will have the default value:
INSERT INTO films (code, title, did, date_prod, kind)
VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
此示例对日期列使用 DEFAULT 子句,而不是指定值:
This example uses the DEFAULT clause for the date columns rather than specifying a value:
INSERT INTO films VALUES
('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');
要插入完全由默认值组成的行:
To insert a row consisting entirely of default values:
INSERT INTO films DEFAULT VALUES;
使用 multirow VALUES 语法插入多行:
To insert multiple rows using the multirow VALUES syntax:
INSERT INTO films (code, title, did, date_prod, kind) VALUES
('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');
此示例将一些行从具有与 films 相同列布局的表 tmp_films 插入到表 films :
This example inserts some rows into table films from a table tmp_films with the same column layout as films:
INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';
此示例插入到数组列:
This example inserts into array columns:
-- Create an empty 3x3 gameboard for noughts-and-crosses
INSERT INTO tictactoe (game, board[1:3][1:3])
VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}');
-- The subscripts in the above example aren't really needed
INSERT INTO tictactoe (game, board)
VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}');
将一行插入到表 distributors 中,并返回 DEFAULT 子句生成的序号:
Insert a single row into table distributors, returning the sequence number generated by the DEFAULT clause:
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;
增加管理 Acme Corporation 帐户的销售人员的销售计数,并将整个更新的行与当前时间一起记录在日志表中:
Increment the sales count of the salesperson who manages the account for Acme Corporation, and record the whole updated row along with current time in a log table:
WITH upd AS (
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')
RETURNING *
)
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
适当地插入或更新新的分销商。假设已经定义了一个唯一索引,用于限制出现在 did 列中的值。注意,特殊的 excluded 表用于引用最初建议插入的值:
Insert or update new distributors as appropriate. Assumes a unique index has been defined that constrains values appearing in the did column. Note that the special excluded table is used to reference values originally proposed for insertion:
INSERT INTO distributors (did, dname)
VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
插入一个分销商,或者在插入行时存在一个现有排除行(包含在行插入触发器触发后匹配限制列或列的行)的情况下,对建议插入的行不采取任何操作。该示例假设已经定义了一个唯一索引,用于限制出现在 did 列中的值:
Insert a distributor, or do nothing for rows proposed for insertion when an existing, excluded row (a row with a matching constrained column or columns after before row insert triggers fire) exists. Example assumes a unique index has been defined that constrains values appearing in the did column:
INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')
ON CONFLICT (did) DO NOTHING;
适当地插入或更新新的分销商。该示例假设已经定义了一个唯一索引,用于限制出现在 did 列中的值。 WHERE 子句用于限制实际更新的行(任何未更新的现有行仍将被锁定):
Insert or update new distributors as appropriate. Example assumes a unique index has been defined that constrains values appearing in the did column. WHERE clause is used to limit the rows actually updated (any existing row not updated will still be locked, though):
-- Don't update existing distributors based in a certain ZIP code
INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')
ON CONFLICT (did) DO UPDATE
SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'
WHERE d.zipcode <> '21201';
-- Name a constraint directly in the statement (uses associated
-- index to arbitrate taking the DO NOTHING action)
INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')
ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;
如果可能的话,插入新的分销商;否则 DO NOTHING 。该示例假设已经定义了一个唯一索引,用于限制出现在 did 列中的值,该行是一个子集,其中 is_active 布尔列评估为 true :
Insert new distributor if possible; otherwise DO NOTHING. Example assumes a unique index has been defined that constrains values appearing in the did column on a subset of rows where the is_active Boolean column evaluates to true:
-- This statement could infer a partial unique index on "did"
-- with a predicate of "WHERE is_active", but it could also
-- just use a regular unique constraint on "did"
INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')
ON CONFLICT (did) WHERE is_active DO NOTHING;
Compatibility
INSERT 符合 SQL 标准,但 RETURNING 子句是 PostgreSQL 扩展,并且 WITH 与 INSERT 一起使用以及使用 ON CONFLICT 指定备用操作的能力也是如此。此外,在省略列名称列表的情况下,但并非所有列都从 VALUES 子句或 query 填充的情况,这是标准不允许的。如果您更喜欢一个更符合 SQL 标准的语句 @{s22」,请参阅 MERGE 。
INSERT conforms to the SQL standard, except that the RETURNING clause is a PostgreSQL extension, as is the ability to use WITH with INSERT, and the ability to specify an alternative action with ON CONFLICT. Also, the case in which a column name list is omitted, but not all the columns are filled from the VALUES clause or query, is disallowed by the standard. If you prefer a more SQL standard conforming statement than ON CONFLICT, see MERGE.
SQL 标准规定,只有在始终存在一个生成的标识列的情况下才能指定 OVERRIDING SYSTEM VALUE 。在任何情况下,PostgreSQL 都允许该子句,如果它不适用,则忽略它。
The SQL standard specifies that OVERRIDING SYSTEM VALUE can only be specified if an identity column that is generated always exists. PostgreSQL allows the clause in any case and ignores it if it is not applicable.
query 子句的可能限制在 SELECT 下作了说明。
Possible limitations of the query clause are documented under SELECT.