How to insert data into a MySQL database?

The SQL INSERT INTO Statement statement is used to insert new data into a table of a MySQL database.

How to use the SQL INSERT INTO

users table in the empty state.
users table in the empty state.
  • Table Nameusers
  • users Table columnsname, age, email.

Now we will see how you can use the INSERT INTO statement to insert data into the users table.

Syntax of INSERT INTO statement

After the table_name, specify the columns in parentheses, and then after the VALUES, specify the values in parentheses for the columns in the same order.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Implementing the above INSERT INTO syntax

A Dummy User’s details
INSERT INTO users (name, age, email)
VALUES ('John Doe', 23, '[email protected]');
After successfully inserting a data
After successfully inserting data

Tip: No need to specify column names If

You don’t need to specify the column names if you add values for all columns. But you have to maintain the order of the values according to the columns.

INSERT INTO users VALUES ('John Doe', 23, '[email protected]');

You can skip one or more columns

If you skip a column name while inserting new data, in that case, the default value will be assigned for the omitted column. Look at the following example –

INSERT INTO users (name, email)
VALUES ('Mark', '[email protected]');
default value is assigned

SQL insert bulk of data OR multiple rows at once

Syntax

Use the following SQL syntax to insert bulk of data OR multiple rows at once.

INSERT INTO users (name, age, email)
VALUES (value_list1),
(value_list2),
(value_list3),
(value_list...);

Example of Inserting multiple rows at once

INSERT INTO users (name, age, email)
VALUES ('Baburao', 53, '[email protected]'),
('Raju', 35, '[email protected]'),
('Shyam', 38, '[email protected]');
Bulk data inserted result