MySQL INSERT: Syntax, Examples, and Common Errors
Learn MySQL INSERT syntax, column lists, multi-row values, defaults, INSERT IGNORE, and fixes for common insert errors.
In MySQL, the INSERT statement is used to insert one or more rows into a table.
INSERT syntax
There are one or more rows in a INSERT statement which will be inserted into a table.
Here is the syntax of the INSERT statement used to insert one row:
INSERT INTO table_name (column_1, column_2, ...)
VALUES (value_1, value_2, ...);
Here is the syntax of the INSERT statement used to insert more rows:
INSERT INTO table_name (column_1, column_2, ...)
VALUES (value_11, value_12, ...),
(value_21, value_22, ...)
...;
In the syntax:
INSERT INTOandVALUESare keywords.table_namespecifies the table name.(column_1, column_2, ...)specifies a list of column names.(value_11, value_12, ...)after theVALUESkeyword specifies a list of values of one row.- The
INSERTstatement returns the number of inserted rows.
This INSERT ... VALUES (...) syntax is different from a standalone VALUES ROW(...) table constructor, which returns a result table; see the MySQL VALUES statement guide. Error 3942 refers to an empty ROW() in that standalone form; see MySQL Error 3942: Empty ROW() in a VALUES Table Constructor.
INSERT examples
Let us use the CREATE TABLE statement to create a table named user as a demonstration:
CREATE TABLE user (
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT,
birthday DATE,
PRIMARY KEY (id)
);
There are four columns in the user table:
- The
idcolumn has aINTdata type, and it is primary key column and has a auto increment value. - The
namecolumn has aVARCHAR(255)data type, and it isNOT NULL. - The
agecolumn has aINTdata type. - The
birthdaycolumn has aDATEdata type.
Insert one row
Let us use the following statment to insert one row into the user table:
INSERT INTO user (name, age)
VALUES ('Jim', 18);
Query OK, 1 row affected (0.00 sec)
Note: The output 1 row affected represents the row has been inserted into the user table.
We can also verify it by selectint rows from the table:
SELECT * FROM user;
+----+------+------+----------+
| id | name | age | birthday |
+----+------+------+----------+
| 1 | Jim | 18 | NULL |
+----+------+------+----------+
1 row in set (0.00 sec)
Notice:
- The value of the
idcolumn is generated automatically as it isAUTO_INCREMENTa column. - The
birthdaycolumn valuesNULL, because we only insertednameandagecolumns.
Insert multiple rows
For detailed examples, column/value matching, and large-batch limits, see MySQL INSERT multiple rows.
Let us use the following statment to insert two rows into the user table:
INSERT INTO user (name, age)
VALUES ('Tim', 19), ('Lucy', 16);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
Notice:
- The output
2 row affectedrepresentative of the two rows have been inserted into theusertable.
We can also verify it by selectint rows from the table:
SELECT * FROM user;
+----+------+------+----------+
| id | name | age | birthday |
+----+------+------+----------+
| 1 | Jim | 18 | NULL |
| 2 | Tim | 19 | NULL |
| 3 | Lucy | 16 | NULL |
+----+------+------+----------+
3 rows in set (0.00 sec)
To insert rows returned by a query instead of literal values, see MySQL INSERT INTO SELECT.
Insert date column
To insert a date type column, you can use a text value with YYYY-MM-DD format. The following is a description of this date format:
YYYYrepresents a four-digit year, for example2020.MMrepresents a two-digit month, for example01,02and12.DDrepresents two-digit dates, for example01,02,30,31.
The following statement to insert a row into the user table with birthday column:
INSERT INTO user(name, age, birthday)
VALUES('Jack', 20, '2000-02-05');
Query OK, 1 row affected (0.00 sec)
Let us see the rows in the user table:
SELECT * FROM user;
+----+------+------+------------+
| id | name | age | birthday |
+----+------+------+------------+
| 1 | Jim | 18 | NULL |
| 2 | Tim | 19 | NULL |
| 3 | Lucy | 16 | NULL |
| 4 | Jack | 20 | 2000-02-05 |
+----+------+------+------------+
4 rows in set (0.00 sec)
INSERT modifier
In MySQL, INSERT statements support 4 modifiers:
-
LOW_PRIORITY: If you specifyLOW_PRIORITYmodifier, MySQL server will delay the execution of theINSERToperation until there are no clients who read on the table.LOW_PRIORITYmodifier is supported by those storage engines which only has table-level locking, such as:MyISAM,MEMORY, andMERGE. -
HIGH_PRIORITY: If you specifyHIGH_PRIORITYmodifier, it will overwrite the server boot--low-priority-updatesoptions.HIGH_PRIORITYmodifier is supported by those storage engines which only has table-level locking, such as:MyISAM,MEMORY, andMERGE. -
IGNORE:INSERT IGNOREskips rows with ignorable errors, often duplicate keys, and reports warnings. It can also adjust invalid values, so inspect the warnings. See MySQL INSERT IGNORE for examples and cautions. -
DELAYED: Deprecated and ignored in MySQL 8.4; do not use it in new statements. See the MySQL 8.4 INSERT reference.
The usage of modifiers is as follows:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
INTO table_name
...
INSERT restrictions
The client and server each enforce a max_allowed_packet limit on messages. If an INSERT statement exceeds either limit, MySQL can return ER_NET_PACKET_TOO_LARGE and close the connection. See the MySQL Packet Too Large note for details.
The following statement shows max_allowed_packet on the current server:
SHOW VARIABLES LIKE 'max_allowed_packet';
+--------------------+----------+
| Variable_name | Value |
+--------------------+----------+
| max_allowed_packet | 67108864 |
+--------------------+----------+
1 row in set (0.00 sec)
It is in bytes, and the value may be different on different servers.
For a CSV file, see Import CSV into MySQL.
Troubleshoot common INSERT errors
| Error | Typical cause | Guide |
|---|---|---|
| 1064 (42000) | MySQL cannot parse the statement’s syntax. | Fix MySQL Error 1064 |
| 1136 (21S01) | A VALUES tuple or SELECT list has a different number of expressions than the target columns. |
Fix MySQL Error 1136 |
| 1364 | Strict SQL mode rejects an omitted required column that has no default. | Fix MySQL Error 1364 |
| 1048 | An insert explicitly supplies NULL for a NOT NULL column. |
Fix MySQL Error 1048 · MySQL NOT NULL |
| 1406 | A value exceeds the target column’s allowed length. | Fix MySQL Error 1406 |
| 1062 | The inserted or updated value conflicts with a primary or unique key. | Fix MySQL Error 1062 |
| 1452 | A child-row foreign key has no matching parent key. | Fix MySQL Error 1452 |
Summary
INSERTadds one or more rows to a table.- Use an explicit column list and provide the matching values; use defaults only when they fit the data model.
- For multi-row inserts, every
VALUEStuple must have the same number of entries. - Use
INSERT IGNOREonly when skipping ignorable row errors is intended, and inspect the warnings.