13.2.6 INSERT Statement语句

13.2.6.1 INSERT ... SELECT Statement语句
13.2.6.2 INSERT ... ON DUPLICATE KEY UPDATE Statement语句
13.2.6.3 INSERT DELAYED Statement语句
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    { {VALUES | VALUE} (value_list) [, (value_list)] ...
      |
      VALUES row_constructor_list
    }
    [AS row_alias[(col_alias [, col_alias] ...)]]
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [AS row_alias[(col_alias [, col_alias] ...)]]
    SET assignment_list
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    [AS row_alias[(col_alias [, col_alias] ...)]]
    {SELECT ... | TABLE table_name}
    [ON DUPLICATE KEY UPDATE assignment_list]
value:
    {expr | DEFAULT}
value_list:
value [, value] ...
row_constructor_list:
    ROW(value_list)[, ROW(value_list)][, ...]
assignment:
col_name = [row_alias.]value
assignment_list:
assignment [, assignment] ...

INSERT inserts new rows into an existing table. INSERT将新行插入到现有表中。The INSERT ... VALUES, INSERT ... VALUES ROW(), and INSERT ... SET forms of the statement insert rows based on explicitly specified values. 语句的INSERT ... VALUESINSERT ... VALUES ROW()INSERT ... SET形式根据显式指定的值插入的行。The INSERT ... SELECT form inserts rows selected from another table or tables. INSERT ... SELECT形式插入从另一个或多个表中选择的行。You can also use INSERT ... TABLE in MySQL 8.0.19 and later to insert rows from a single table. 您也可以MySQL8.0.19及更高版本中使用INSERT ... TABLE来插入单个表中的行。INSERT with an ON DUPLICATE KEY UPDATE clause enables existing rows to be updated if a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY. INSERT配合ON DUPLICATE KEY UPDATE子句启用了在插入的行会导致UNIQUE索引或PRIMARY KEY中的值重复的情况下更新现有的行。In MySQL 8.0.19 and later, a row alias with one or more optional column alises can be used with ON DUPLICATE KEY UPDATE to refer to the row to be inserted.在MySQL 8.0.19及更高版本中,可以将带有一个或多个可选列alise的行别名与ON DUPLICATE KEY UPDATE一起使用,以引用要插入的行。

For additional information about INSERT ... SELECT and INSERT ... ON DUPLICATE KEY UPDATE, see Section 13.2.6.1, “INSERT ... SELECT Statement”, and Section 13.2.6.2, “INSERT ... ON DUPLICATE KEY UPDATE Statement”.有关INSERT ... SELECTINSERT ... ON DUPLICATE KEY UPDATE的其他信息,请参见第13.2.6.1节,“INSERT ... SELECT语句”第13.2.6.2节,“INSERT ... ON DUPLICATE KEY UPDATE语句”

In MySQL 8.0, the DELAYED keyword is accepted but ignored by the server. 在MySQL 8.0中,DELAYED关键字被服务器接受但忽略。For the reasons for this, see Section 13.2.6.3, “INSERT DELAYED Statement”,有关原因,请参见第13.2.6.3节,“插入延迟声明”

Inserting into a table requires the INSERT privilege for the table. 插入到表中需要表的INSERT权限。If the ON DUPLICATE KEY UPDATE clause is used and a duplicate key causes an UPDATE to be performed instead, the statement requires the UPDATE privilege for the columns to be updated. 如果使用了ON DUPLICATE KEY UPDATE子句,而重复键导致执行UPDATE,则语句需要被更新列的UPDATE权限。For columns that are read but not modified you need only the SELECT privilege (such as for a column referenced only on the right hand side of an col_name=expr assignment in an ON DUPLICATE KEY UPDATE clause).对于已读取但未修改的列,您只需要SELECT权限(例如对于仅在ON DUPLICATE KEY UPDATE子句中col_name=expr赋值右侧引用的列)。

When inserting into a partitioned table, you can control which partitions and subpartitions accept new rows. 在插入分区表时,可以控制哪些分区和子分区接受新行。The PARTITION clause takes a list of the comma-separated names of one or more partitions or subpartitions (or both) of the table. PARTITION子句获取表中一个或多个分区或子分区(或两者)的逗号分隔名称列表。If any of the rows to be inserted by a given INSERT statement do not match one of the partitions listed, the INSERT statement fails with the error Found a row not matching the given partition set. 如果给定INSERT语句要插入的任何行与列出的某个分区不匹配,则INSERT语句将失败,并出现“发现与给定分区集不匹配的行”错误。For more information and examples, see Section 24.5, “Partition Selection”.有关更多信息和示例,请参阅第24.5节,“分区选区”

tbl_name is the table into which rows should be inserted. tbl_name是应该插入行的表。Specify the columns for which the statement provides values as follows:指定语句为其提供值的列,如下所示:

Column values can be given in several ways:列值可以通过几种方式给出:

INSERT statements that use VALUES syntax can insert multiple rows. 使用VALUES语法的INSERT语句可以插入多行。To do this, include multiple lists of comma-separated column values, with lists enclosed within parentheses and separated by commas. 为此,请包含多个以逗号分隔的列值列表,列表用括号括起来,并用逗号分隔。Example:例如:

INSERT INTO tbl_name (a,b,c)
    VALUES(1,2,3), (4,5,6), (7,8,9);

Each values list must contain exactly as many values as are to be inserted per row. 每个值列表必须包含与每行插入的值相同的值。The following statement is invalid because it contains one list of nine values, rather than three lists of three values each:以下语句无效,因为它包含一个包含九个值的列表,而不是三个分别包含三个值的列表:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3,4,5,6,7,8,9);

VALUE is a synonym for VALUES in this context. VALUE在这个上下文中是VALUES的同义词。Neither implies anything about the number of values lists, nor about the number of values per list. 既不表示任何关于值列表的数量,也不表示每个列表的值的数量。Either may be used whether there is a single values list or multiple lists, and regardless of the number of values per list.无论是单个值列表还是多个列表,也不管每个列表的值数是多少,都可以使用这两种方法。

INSERT statements using VALUES ROW() syntax can also insert multiple rows. 使用VALUES ROW()语法的INSERT语句也可以插入多行。In this case, each value list must be contained within a ROW() (row constructor), like this:在这种情况下,每个值列表必须包含在ROW()中(ROW构造函数),如下所示:

INSERT INTO tbl_name (a,b,c)
    VALUES ROW(1,2,3), ROW(4,5,6), ROW(7,8,9);

The affected-rows value for an INSERT can be obtained using the ROW_COUNT() SQL function or the mysql_affected_rows() C API function. 插入的受影响行值可以使用ROW_COUNT()SQL函数或mysql_impacted_rows()C API函数获得。See Section 12.16, “Information Functions”, and mysql_affected_rows().请参阅第12.16节,“信息函数”mysql_affected_rows()

If you use INSERT ... VALUES or INSERT ... VALUES ROW() with multiple value lists, or INSERT ... SELECT or INSERT ... TABLE, the statement returns an information string in this format:如果使用INSERT ... VALUESINSERT ... VALUES ROW()配合多个值列表,或使用INSERT ... SELECTINSERT ... TABLE,则语句返回以下格式的信息字符串:

Records: N1 Duplicates: N2 Warnings: N3

If you are using the C API, the information string can be obtained by invoking the mysql_info() function. 如果您使用的是C API,那么可以通过调用mysql_info()函数来获取信息字符串。See mysql_info().请参阅mysql_info()

Records indicates the number of rows processed by the statement. Records指示语句处理的行数。(This is not necessarily the number of rows actually inserted because Duplicates can be nonzero.) (这不一定是实际插入的行数,因为Duplicates可以不为零。)Duplicates indicates the number of rows that could not be inserted because they would duplicate some existing unique index value. Duplicates表示由于重复某些现有唯一索引值而无法插入的行数。Warnings indicates the number of attempts to insert column values that were problematic in some way. Warnings表示尝试插入列值的次数,这些列值在某种程度上有问题。Warnings can occur under any of the following conditions:在以下任何情况下都可能出现警告:

The INSERT statement supports the following modifiers:INSERT语句支持以下修饰符:

13.2.6.1 INSERT ... SELECT Statement
13.2.6.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
13.2.6.3 INSERT DELAYED Statement