MySQL INSERT usages and examples
In this article, we will use MySQL INSERT statement to insert one or more rows into a table.
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.
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
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)
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: If you specifyIGNOREmodifier, MySQL server will perform ignore those errors can be ignored during theINSERToperation. These errors return asWARNING. -
DELAYED: This modifier has been deprecated in MySQL 5.6 and will be removed in the future. In MySQL 8.0, this modifier is available but will be ignored.
The usage of modifiers is as follows:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
INTO table_name
...
INSERT restrictions
In MySQL, the upper limit of the size of any single message on the server and client is configured by max_allowed_packet. When a SELECT size of the statement exceeds max_allowed_packet value, the server will give an error.
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.
Conclusion
In this article, you learned MySQL INSERT syntax and usages by examples. The following are the main points of the INSERT statement:
- The
INSERTstatement is used to insert one or more rows into table. - If you want to ignore errors during inserting, you can use the
IGNOREmodifier.