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#
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#
| Name | Meaning | Default |
|---|---|---|
IGNORE | Accepted. | off |
col_name list | Columns that receive the values. () (empty) is allowed. Without a list the row takes all visible columns in table order. | all visible columns |
DEFAULT | The column's default (or its generated or auto-increment value). | none |
ON DUPLICATE KEY UPDATE | If a unique or primary key is taken, update the existing row instead. | none |
RETURNING | Return 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 formINSERT ... VALUES ... AS new. A statement that uses them is a syntax error (1064). - The values of
INSERT ... SELECTare 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,CHECKconstraints (error 3819), unique keys (1062) and foreign keys (1452).sql_modecan be changed to make conversion non-strict. - A multi-row
VALUESstatement 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 UPDATEruns theAFTER UPDATEtriggers for the updated row. TheBEFOREtriggers of that update are not run.- In
INSERT ... SELECT ... ON DUPLICATE KEY UPDATEan 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 IGNOREskips rows that break a unique key. For aCHECKviolation it stops with the error where MySQL skips the row with a warning.- Privileges:
INSERTon the table (on the named columns if the grant is by column);UPDATEforON DUPLICATE KEY UPDATE;SELECTfor what the query reads. - Triggers:
BEFORE INSERTruns before each row is written,AFTER INSERTafter the statement has written all its rows. See CREATE TRIGGER.
Examples#
Example:
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;