SQL reference (MySQL and MariaDB dialects)

INSERT statement

INSERT with VALUES, SET and SELECT, IGNORE, ON DUPLICATE KEY UPDATE and RETURNING, as the Raft DB parser accepts it.

INSERT adds rows to a table. A statement is all or nothing: if one row fails, the rows already written by the statement are taken back.

Syntax#

SQL
INSERT [IGNORE] [INTO] [db_name.]tbl_name [(col_name [, col_name] ...)]
    { { VALUES | VALUE } (value_list) [, (value_list)] ...
    | query_expression }
    [ON DUPLICATE KEY UPDATE assignment_list]
    [RETURNING select_item [, select_item] ...]

INSERT [IGNORE] [INTO] [db_name.]tbl_name
    SET col_name = value [, col_name = value] ...
    [ON DUPLICATE KEY UPDATE assignment_list]
    [RETURNING select_item [, select_item] ...]

value:
    expr | DEFAULT

assignment_list:
    [tbl_name.]col_name = value [, [tbl_name.]col_name = value] ...

query_expression is a SELECT, a parenthesised query, or a UNION / EXCEPT / INTERSECT of them (see SELECT).

Arguments#

NameMeaningDefault
IGNOREAccepted.off
col_name listColumns that receive the values. () (empty) is allowed. Without a list the row takes all visible columns in table order.all visible columns
DEFAULTThe column's default (or its generated or auto-increment value).none
ON DUPLICATE KEY UPDATEIf a unique or primary key is taken, update the existing row instead.none
RETURNINGReturn the written rows as a result set. See RETURNING.none

Remarks#

  • Not in the grammar: LOW_PRIORITY, HIGH_PRIORITY, DELAYED, PARTITION (...), and the row alias form INSERT ... VALUES ... AS new. A statement that uses them is a syntax error (1064).
  • The values of INSERT ... SELECT are read before anything is written, so the query may read the table it inserts into.
  • Every row is checked for: column conversion (strict, as MySQL's strict sql_mode), generated columns, CHECK constraints (error 3819), unique keys (1062) and foreign keys (1452). sql_mode can be changed to make conversion non-strict.
  • A multi-row VALUES statement into a table without unique indexes, foreign keys or checks is sent to Raft DB together; the effect is the same as row by row.
  • INSERT ... ON DUPLICATE KEY UPDATE runs the AFTER UPDATE triggers for the updated row. The BEFORE triggers of that update are not run.
  • In INSERT ... SELECT ... ON DUPLICATE KEY UPDATE an unqualified column of the update list is taken as the target's column; MySQL calls it ambiguous (error 1052) when the select reads a column of that name.
  • A write that reads its own table in a subquery gives error 1093, as MySQL does.
  • INSERT IGNORE skips rows that break a unique key. For a CHECK violation it stops with the error where MySQL skips the row with a warning.
  • Privileges: INSERT on the table (on the named columns if the grant is by column); UPDATE for ON DUPLICATE KEY UPDATE; SELECT for what the query reads.
  • Triggers: BEFORE INSERT runs before each row is written, AFTER INSERT after the statement has written all its rows. See CREATE TRIGGER.

Examples#

Example:

SQL
INSERT INTO cust VALUES (1,'Ann','Oslo'),(2,'Bob','Rome');
INSERT INTO t (name, qty) VALUES ('a', 1), ('b', 2);
INSERT INTO t SET name = 'f';
INSERT INTO t (name) SELECT CONCAT(name, name) FROM t WHERE qty = 1;
INSERT INTO t VALUES (1, 1, 1, 'zz') ON DUPLICATE KEY UPDATE qty = -1;
INSERT INTO t (name, qty) VALUES ('a', 1), ('b', 2) RETURNING id, name;

See also#