MySQL INSERT
In MySQL, the INSERT statement is used to insert a single or multiple records into a table in a database.
Syntax:
INSERT into table_name(column_1, column_2, ... column_n ) VALUES(value1, value2, .. valuen);
Example 1: MySQL INSERT single record insertion: Items table before insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
INSERT INTO items (id, name, quantity) VALUES (3, 'Fashion’, 100);
Explanation:
The ‘items’ is an already existing table with some records. Here we are inserting a new row under the respective columns with the corresponding values: 3, ‘Fashion’, and 100.
Items table after insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
Example 2: MySQL INSERT partial record insertion: Items table before insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
INSERT INTO items (id, name) VALUES (3, 'Fashion’);
Explanation:
The ‘items’ is an already existing table with some records. We are inserting a new row with the corresponding values: 3 and ‘Fashion’ in the ‘id’ and ‘name’ columns respectively.
Items table after insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | NULL |
Example 3: MySQL INSERT multiple record insertion: Items table before insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
INSERT INTO items (id, name, quantity) VALUES (3, 'Fashion’, 100) (4, ‘Grocery’, 90) (5, ‘Toys’, 50);
Explanation:
The ‘items’ is an already existing table with some records. Here we insert multiple new records under the respective columns with the corresponding values.
Items table after insertion:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
4 | Grocery | 90 |
5 | Toys | 50 |