MySQL Insert Syntax

Destination Table: Provide fully qualifies Table name in which you want to insert records

Column1…ColumnN: It allows us to choose the number of columns from the table. It may be One or more.

Column1_Value…ColumnN_Value: Please specify the values that you want to insert. For instance, Column1_Value will be inserted in Column1. If you omit the field names, you must specify the values (i.e., filed values) in the order defined by the destination table structure

If you are inserting values for all the column in destination table then we can simply write as:

TIP: It is not good practice to ignore the column names in insert statement. So, always provide the column names.

Above queries will insert data into First_Name, Last_Name, Education, Occupation, Yearly_Income, and Sales columns. Whenever you omit the field names, you must specify the column_values in the order defined by the destination table structure.

OUTPUT

MySQL Insert Multiple Rows

In this example, We are going to insert multiple rows into customer Table in more traditional way.

MySQL Insert few columns example

It is not mandatory to insert all the column values at one go. You can omit few column while inserting, and update them later. In this MySQL insert statement example, We are going to insert few column values into company table.

Above Insert query will insert data into First_Name, Last name, Education, and Sales columns. NULL values will be inserted for the remaining columns

TIP: When you are inserting few records into the table, you must specify the column names.

OUTPUT

MySQL Insert Into Example

Instead of hard coding all the values, you can use INSERT INTO SELECT Statement to insert records from one table to another. In this example, We are going to select rows from the country table, and insert them into countrydetails table. Here we are going to restrict the rows using the WHERE Clause.

We are not going to explain the code implementation at this point, I suggest you to refer Insert Into Select Statement article. Let us see whether the query inserted the selected data into the destination table or not

OUTPUT

MySQL Insert Example – Command Prompt

In this example, We are going to insert one record into sales details table using command prompt. Before we get into the query, the data inside our table is as shown below