🎓 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 REPLACE statement in MySQL.
The REPLACE statement is used to insert a new row into a table or update an existing row if a row with the same primary key or unique key already exists. This command is useful for situations where you want to ensure that the row exists with the latest data.
We will cover the syntax, examples, and important considerations for using the REPLACE statement.
REPLACE Query
To insert or replace data in a table, we use the REPLACE INTO statement.
Syntax
REPLACE INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);
table_name: The name of the table where you want to insert or replace data.column1, column2, ..., columnN: The names of the columns where you want to insert or replace data.value1, value2, ..., valueN: The values to insert or replace into the columns.
Example
REPLACE INTO employees (id, first_name, last_name, email, hire_date) VALUES (1, 'Rahul', 'Sharma', 'rahul.sharma@newemail.com', '2023-07-01');
This example inserts a new row into the employees table or updates the existing row with id 1 if it already exists.
REPLACE with Unique Key
The REPLACE statement can also be used with a unique key to ensure that a row is either inserted or updated based on the unique key.
Example
REPLACE INTO employees (email, first_name, last_name, hire_date) VALUES ('rahul.sharma@newemail.com', 'Rahul', 'Sharma', '2023-07-01');
This example uses the email column, which is a unique key, to insert or replace the row in the employees table.
Full Example
Let's go through a full example where we create a table, insert data into it, and perform various REPLACE queries.
- 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');
- REPLACE Query to Insert a New Row:
REPLACE INTO employees (id, first_name, last_name, email, hire_date) VALUES (3, 'Amit', 'Kumar', 'amit.kumar@example.com', '2023-07-03');
- REPLACE Query to Update an Existing Row:
REPLACE INTO employees (id, first_name, last_name, email, hire_date) VALUES (1, 'Rahul', 'Sharma', 'rahul.sharma@newemail.com', '2023-07-01');
- Query the Data:
SELECT * FROM employees;
Output
| id | first_name | last_name | hire_date | |
|---|---|---|---|---|
| 1 | Rahul | Sharma | rahul.sharma@newemail.com | 2023-07-01 |
| 2 | Priya | Singh | priya.singh@example.com | 2023-07-02 |
| 3 | Amit | Kumar | amit.kumar@example.com | 2023-07-03 |
Important Considerations
- Data Loss: The
REPLACEstatement deletes the old row and inserts a new row. This can lead to data loss if there are any columns not included in theREPLACEstatement. - Constraints: Ensure that the values you insert or replace comply with any constraints such as
NOT NULL,UNIQUE, andPRIMARY KEY. - Performance: Using
REPLACEcan be less efficient than usingINSERTorUPDATEseparately, especially on tables with many rows or complex indexes.
Conclusion
The REPLACE statement in MySQL is used for inserting or updating rows in a table based on the presence of a primary key or unique key. This chapter covered how to use the REPLACE INTO statement, including examples of inserting new rows and updating existing rows.
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