MySQL : INSERT INTO Statement

In MySQL, the INSERT INTO statement is used to add new rows of data into a table. It allows you to specify the values to be inserted into specific columns of the table. Here's the basic syntax of the INSERT INTO statement:


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

 

Where:

  • table_name is the name of the table into which you want to insert data.
  • (column1, column2, ...) is an optional list of columns in the table where data will be inserted. If you omit this list, you must specify a value for every column in the table, in the same order as they were defined.
  • VALUES (value1, value2, ...) specifies the values to be inserted into the corresponding columns. The values must be provided in the same order as the columns listed.

Here's an example:


INSERT INTO employees (first_name, last_name, department, salary)
VALUES ('John', 'Doe', 'Sales', 50000);

 

This query will insert a new row into the employees table with values 'John' for the first_name column, 'Doe' for the last_name column, 'Sales' for the department column, and 50000 for the salary column.

You can also insert multiple rows into a table with a single INSERT INTO statement by specifying multiple sets of values separated by commas:


INSERT INTO employees (first_name, last_name, department, salary)
VALUES ('Jane', 'Smith', 'Marketing', 60000),
       ('Bob', 'Johnson', 'IT', 70000),
       ('Emily', 'Brown', 'Finance', 55000);

This query will insert three new rows into the employees table with the specified values.

The INSERT INTO statement is a fundamental part of SQL and is used extensively for adding data to database tables.