MySQL INSERT INTO Statement
Last Updated :
19 Jun, 2024
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database.
Understanding how to use the INSERT INTO statement is key for managing and ensuring the integrity and functionality of the database.
INSERT INTO Statement
The INSERT INTO
statement in MySQL is a Data Manipulation Language (DML) command that allows users to add new records (rows) into a specified table. It follows a concise syntax to specify the table name and the values to be inserted into the respective columns.
Let's look at Various ways of using the INSERT INTO statement, exploring how it can be used to add data efficiently to our table.
- Insert record in a single row
- Insert record in multiple rows
Syntax:
There are two ways to write an INSERT INTO statement in MySQL.
1. The general syntax for inserting a single record into a MySQL table using the SQL INSERT INTO command is as follows:
INSERT INTO table_name (column1, column2, ..., columnN)
VALUES (value1, value2, ..., valueN);
2. Use the following statement to insert multiple records with a single command:
INSERT INTO table_name (column1, column2, ..., columnN)
VALUES
(value1_1, value1_2, ..., value1_N),
(value2_1, value2_2, ..., value2_N),
...,
(valueM_1, valueM_2, ..., valueM_N);
Demo MySQL Database
Write the following command to create an empty table on which we will perform the INSERT INTO.
CREATE TABLE employees (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
INSERT Statement Examples
Let's look at the examples of the INSERT INTO statement, to understand the workings of INSERT INTO statement, and cover its different use cases.
Example 1: MySQL INSERT INTO Table Single Row
To insert a single record into a table, specify the table name and provide values for each column in the order they appear in the table.
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Query:
-- Insert single record into the employees table
INSERT INTO employees (name, age, department)
VALUES ('John Doe', 30, 'IT');
-- Select all records from the employees table
select * from employees;
Output:
name
| age
| department
|
---|
John Doe
| 30
| IT
|
Explanation: This syntax inserts a single row of data into the 'employees' table. You specify the table name and the columns into which you want to insert data, followed by the corresponding values. In this example, we insert the name 'John Doe', age '30', and department 'IT' into the respective columns.
Example 2: MySQL INSERT INTO table Multiple Rows
MySQL allows inserting multiple rows of data in a single query. It is reducing the overhead of executing multiple queries. The syntax is similar to inserting a single row but with multiple sets of values enclosed within parentheses and separated by commas
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES
(value1_1, value1_2, value1_3, ...),
(value2_1, value2_2, value2_3, ...),
(value3_1, value3_2, value3_3, ...);
Query:
-- Insert multiple records into the employees table
INSERT INTO employees (name, age, department)
VALUES
('Jane Smith', 28, 'HR'),
('Michael Johnson', 35, 'Finance'),
('Emily Brown', 32, 'Marketing');
-- Select all records from the employees table
SELECT * FROM employees;
Output:
name
| age
| department
|
---|
Jane Smith
| 28
| HR
|
Michael Johnson
| 35
| Finance
|
Emily Brown
| 32
| Marketing
|
Explanation: This syntax allows you to insert multiple rows of data into the 'employees' table in a single query. Each set of values represents a separate row to be inserted. In this example, we insert three new employees into the table with their respective details.
Example 3: MySQL INSERT INTO SELECT
Sometimes, We may need to insert data into a table based on the results of a SELECT query. MySQL facilitates this through the INSERT INTO SELECT statement. This statement selects data from one table and inserts it into another
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM another_table
WHERE condition;
Query:
-- Create the employees table
CREATE TABLE employees (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
-- Insert multiple records into the employees table
INSERT INTO employees (name, age, department)
VALUES
('Jane Smith', 28, 'HR'),
('Michael Johnson', 35, 'Finance'),
('Emily Brown', 32, 'Marketing');
-- Create the employees_backup table
CREATE TABLE employees_backup (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
-- Insert records into the employees_backup table where age > 30
INSERT INTO employees_backup (name, age, department)
SELECT name, age, department
FROM employees
WHERE age > 30;
-- Select all records from the employees table
SELECT * FROM employees_backup;
Output:
name | age | department |
---|
Michael Johnson | 35 | Finance |
Emily Brown | 32 | Marketing |
Explanation: This syntax inserts data into the 'employees_backup' table based on the results of a SELECT query. In this example, we select employees from the 'employees' table where the age is greater than 30 and insert their details into the 'employees_backup' table.
Example 4: MySQL INSERT INTO DATE
Inserting date values into a MySQL database is a common requirement. MySQL provides several date and time functions to handle date-related operations efficiently. To insert the current date into a date column, we can use the NOW() function with MySQL Insert Date.
Syntax:
INSERT INTO table_name (date_column)
VALUES (NOW());
Query:
INSERT INTO orders (order_date)
VALUES (NOW());
Output:
2024-02-20 12:00:00
Explanation: To insert the current date into a date column, you can use the NOW() function within the INSERT INTO statement. This example inserts the current date into the 'order_date' column of the 'orders' table.
Conclusion
In conclusion, INNER JOIN in MySQL is essential for combining rows from multiple tables based on a common column, ensuring only matching rows are included in the results. This article has explained the basic syntax and usage, showing examples with GROUP BY, HAVING, WHERE, and USING clauses. Mastering INNER JOIN allows you to perform complex queries and manage database information efficiently, making it a crucial skill for effective database management.
Similar Reads
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
MySQL INSERT INTO SELECT Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. INSERT INTO SELECT statement i
5 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 min read
PL/SQL INSERT Statement
The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
3 min read
SELECT INTO in MySQL
MySQL is an open-source Relational Database Management System that stores data in a structured format using rows and columns. MySQL is a platform-independent language and can be compiled on any platform. MySQL is generally used for storing, manipulating, and retrieving data in RDBMS by using the SQL
5 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match. In this article, we will learn how IN operator works and provide practical examples to help you b
4 min read
Python MySQL - Insert into Table
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify
3 min read
MariaDB INSERT Statement
MariaDB is a famous open-source relational database management system. It is renowned for its performance, scalability, and robust functions. One essential thing about operating with databases is the ability to insert information, and the INSERT statement plays a crucial role in this technique. In t
3 min read
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read
PL/SQL INSERT INTO SELECT
In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes. In this guide, we
5 min read