INSERT

This statement inserts new rows into a table.

Synopsis

InsertIntoStmt

INSERT - 图1

TableOptimizerHints

INSERT - 图2

PriorityOpt

INSERT - 图3

IgnoreOptional

INSERT - 图4

IntoOpt

INSERT - 图5

TableName

INSERT - 图6

PartitionNameListOpt

INSERT - 图7

InsertValues

INSERT - 图8

OnDuplicateKeyUpdate

INSERT - 图9

  1. InsertIntoStmt ::=
  2. 'INSERT' TableOptimizerHints PriorityOpt IgnoreOptional IntoOpt TableName PartitionNameListOpt InsertValues OnDuplicateKeyUpdate
  3. TableOptimizerHints ::=
  4. hintComment?
  5. PriorityOpt ::=
  6. ( 'LOW_PRIORITY' | 'HIGH_PRIORITY' | 'DELAYED' )?
  7. IgnoreOptional ::=
  8. 'IGNORE'?
  9. IntoOpt ::= 'INTO'?
  10. TableName ::=
  11. Identifier ( '.' Identifier )?
  12. PartitionNameListOpt ::=
  13. ( 'PARTITION' '(' Identifier ( ',' Identifier )* ')' )?
  14. InsertValues ::=
  15. '(' ( ColumnNameListOpt ')' ( ValueSym ValuesList | SelectStmt | '(' SelectStmt ')' | UnionStmt ) | SelectStmt ')' )
  16. | ValueSym ValuesList
  17. | SelectStmt
  18. | UnionStmt
  19. | 'SET' ColumnSetValue? ( ',' ColumnSetValue )*
  20. OnDuplicateKeyUpdate ::=
  21. ( 'ON' 'DUPLICATE' 'KEY' 'UPDATE' AssignmentList )?

Examples

  1. mysql> CREATE TABLE t1 (a INT);
  2. Query OK, 0 rows affected (0.11 sec)
  3. mysql> CREATE TABLE t2 LIKE t1;
  4. Query OK, 0 rows affected (0.11 sec)
  5. mysql> INSERT INTO t1 VALUES (1);
  6. Query OK, 1 row affected (0.02 sec)
  7. mysql> INSERT INTO t1 (a) VALUES (1);
  8. Query OK, 1 row affected (0.01 sec)
  9. mysql> INSERT INTO t2 SELECT * FROM t1;
  10. Query OK, 2 rows affected (0.01 sec)
  11. Records: 2 Duplicates: 0 Warnings: 0
  12. mysql> SELECT * FROM t1;
  13. +------+
  14. | a |
  15. +------+
  16. | 1 |
  17. | 1 |
  18. +------+
  19. 2 rows in set (0.00 sec)
  20. mysql> SELECT * FROM t2;
  21. +------+
  22. | a |
  23. +------+
  24. | 1 |
  25. | 1 |
  26. +------+
  27. 2 rows in set (0.00 sec)
  28. mysql> INSERT INTO t2 VALUES (2),(3),(4);
  29. Query OK, 3 rows affected (0.02 sec)
  30. Records: 3 Duplicates: 0 Warnings: 0
  31. mysql> SELECT * FROM t2;
  32. +------+
  33. | a |
  34. +------+
  35. | 1 |
  36. | 1 |
  37. | 2 |
  38. | 3 |
  39. | 4 |
  40. +------+
  41. 5 rows in set (0.00 sec)

MySQL compatibility

The INSERT statement in TiDB is fully compatible with MySQL. If you find any compatibility differences, report a bug.

See also