MySQL is a popular open-source relational database management system widely used for web development. It is fast, reliable, and scalable with the ability to handle large amounts of data efficiently. One of the essential operations in MySQL is inserting data into a table. In this article, we will discuss the MySQL INSERT statement, one of the basic queries every developer should know.
What is MySQL INSERT Statement?
The MySQL INSERT statement is a SQL command used to insert one or more rows of data into a table. It specifies the table name, the columns you want to insert data into, and the values you want to insert. The basic syntax for the INSERT statement is as follows:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
In this syntax, table_name
is the name of the table we want to insert data into. column1, column2, column3, ...
represents the names of the columns in the table that we want to insert data into. value1, value2, value3, ...
represent the actual values we want to insert into the corresponding columns.
Inserting Data into a MySQL Table
Let’s see some examples of inserting data into a MySQL table using the INSERT statement. Suppose we have a table named employees
with the following structure.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
Inserting a Single Row
To insert a single row of data into the employees
table, we can use the following command.
INSERT INTO employees (id, name, age, salary)
VALUES (1, 'John Doe', 30, 5000.00);
This statement will insert a single row of data into the employees
table with the values id=1
, name='John Doe'
, age=30
, and salary=5000.00
.
Inserting Multiple Rows
To insert multiple rows of data into the employees
table, we can use the INSERT statement with multiple value sets as shown below.
INSERT INTO employees (id, name, age, salary)
VALUES
(2, 'Jane Doe', 25, 4000.00),
(3, 'Bob Smith', 40, 6000.00),
(4, 'Alice Brown', 35, 5500.00);
This statement will insert three rows of data into the employees
table with the values shown above.
Inserting Data from Another Table
We can also insert data into a MySQL table from another table using the INSERT INTO statement.
Suppose we have another table named new_employees
with the following structure.
CREATE TABLE new_employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL
);
We can insert data from the new_employees
table into the employees
table as shown below.
INSERT INTO employees (id, name, age, salary)
SELECT id, name, age, 0.00
FROM new_employees;
This statement will insert the data from the new_employees
table into the employees
table with the salary column initialized to 0.00 for all rows.
Conclusion
The MySQL INSERT statement is a powerful command for adding data to a table. We can insert a single row, multiple rows, or data from another table into a target table using this statement. It is a fundamental operation that every MySQL developer should know. We hope you found this article helpful in learning how to use the MySQL INSERT statement.
📕 Related articles about MySQL
- Understanding MySQL Data Definition Statements: A Comprehensive Guide
- MySQL DROP TABLE Statement: A Comprehensive Guide
- The Ultimate Guide to MySQL DELETE Statement
- mysql — The MySQL Command-Line Client
- MySQL ALTER FUNCTION Statement
- How to use UNIQUE Constraint in SQL