🎓 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 TRUNCATE TABLE statement in MySQL. Truncating a table removes all rows from the table, but unlike the DROP TABLE statement, it does not delete the table itself. This operation is faster than deleting rows one by one, and it resets any auto-increment counters. We will cover the syntax and examples for using the TRUNCATE TABLE statement.
Truncating a Table
To truncate a table, we use the TRUNCATE TABLE statement. This command removes all rows from the specified table, effectively emptying it.
Syntax
TRUNCATE TABLE table_name;
table_name: The name of the table you want to truncate.
Example
TRUNCATE TABLE employees;
This example removes all rows from the employees table.
Full Example
Let's go through a full example where we create a table, insert some data, truncate the table, and then verify that the table has been emptied.
- 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');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Priya', 'Singh', 'priya.singh@example.com', '2023-07-02');
- Verify the Data Insertion:
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 |
- Truncate the Table:
TRUNCATE TABLE employees;
- Verify the Table Truncation:
SELECT * FROM employees;
Output
| id | first_name | last_name | hire_date | |
|---|---|---|---|---|
Important Considerations
- Irreversibility: Truncating a table is permanent and cannot be undone. Make sure you really want to remove all data from the table before executing the command.
- Performance:
TRUNCATE TABLEis faster thanDELETE FROM table_namebecause it does not generate individual row delete operations. - Auto-Increment Reset: Truncating a table resets any auto-increment counters.
- Permissions: You need the appropriate privileges to truncate a table. Typically, this requires
DROPandALTERprivileges on the table.
Conclusion
Truncating a table in MySQL is a quick way to remove all rows from a table while preserving the table structure. This chapter covered how to use the TRUNCATE TABLE statement and provided a full example to illustrate the process.
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