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 Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read