🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will learn how to use the INSERT statement in MySQL to add data to a table. The INSERT statement is used to insert new rows into a table. We will cover the syntax, examples, and important considerations for using the INSERT statement.
Inserting Data into a Table
To insert data into a table, we use the INSERT INTO statement. This command adds new rows to the specified table.
Syntax
INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);
table_name: The name of the table where you want to insert data.column1, column2, ..., columnN: The names of the columns where you want to insert data.value1, value2, ..., valueN: The values to insert into the columns.
Example
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Rahul', 'Sharma', 'rahul.sharma@example.com', '2023-07-01');
This example inserts a new row into the employees table with the specified values.
Inserting Multiple Rows
You can also insert multiple rows into a table in a single INSERT statement by separating the values with commas.
Syntax
INSERT INTO table_name (column1, column2, ..., columnN) VALUES
(value1, value2, ..., valueN),
(value1_2, value2_2, ..., valueN_2),
...;
Example
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES
('Rahul', 'Sharma', 'rahul.sharma@example.com', '2023-07-01'),
('Priya', 'Singh', 'priya.singh@example.com', '2023-07-02');
This example inserts two new rows into the employees table.
Inserting Data into Specific Columns
If you do not want to insert data into all columns, you can specify only the columns you want to insert data into. The remaining columns will take their default values.
Syntax
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Example
INSERT INTO employees (first_name, email) VALUES ('Amit', 'amit.kumar@example.com');
This example inserts a new row into the employees table, setting values only for the first_name and email columns. The other columns will take their default values.
Inserting Data Without Specifying Column Names
You can also insert data without specifying the column names if you are providing values for all columns in the exact order they are defined in the table.
Syntax
INSERT INTO table_name VALUES (value1, value2, ..., valueN);
Example
INSERT INTO employees VALUES (NULL, 'Neha', 'Verma', 'neha.verma@example.com', '2023-07-03');
This example inserts a new row into the employees table with values for all columns. The NULL value is used for the id column, which is an auto-increment column.
Full Example
Let's go through a full example where we create a table, insert data into it, and then query the data to verify the insertion.
- Create a Database:
CREATE DATABASE company;
- Select the Database:
USE company;
- Create a Table:
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
hire_date DATE
);
- Insert Data into the Table:
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES
('Rahul', 'Sharma', 'rahul.sharma@example.com', '2023-07-01'),
('Priya', 'Singh', 'priya.singh@example.com', '2023-07-02'),
('Amit', 'Kumar', 'amit.kumar@example.com');
- Insert Data Without Specifying Column Names:
INSERT INTO employees VALUES
(NULL, 'Neha', 'Verma', 'neha.verma@example.com', '2023-07-03');
- Query the Data:
SELECT * FROM employees;
Output
| id | first_name | last_name | hire_date | |
|---|---|---|---|---|
| 1 | Rahul | Sharma | rahul.sharma@example.com | 2023-07-01 |
| 2 | Priya | Singh | priya.singh@example.com | 2023-07-02 |
| 3 | Amit | Kumar | amit.kumar@example.com | NULL |
| 4 | Neha | Verma | neha.verma@example.com | 2023-07-03 |
Important Considerations
- Data Types: Ensure that the values you insert match the data types of the columns.
- Constraints: Be aware of constraints such as
NOT NULL,UNIQUE, andPRIMARY KEYthat may affect your ability to insert data. - Default Values: If you do not specify a value for a column, it will take its default value if one is defined.
Conclusion
Inserting data into a table is a fundamental operation in MySQL. This chapter covered how to use the INSERT INTO statement to add new rows to a table, including examples of inserting single and multiple rows, inserting data into specific columns, and inserting data without specifying column names. In the next chapter, we will learn how to select the data from tables.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment