MySQL

How to delete all rows from a table in MySQL

In this tutorial, we are going to see how to delete all rows from a table in MySQL. TRUNCATE TABLE statement deletes all rows from a table in MySQL. It performs the same function as DELETE statement without the WHERE clause.

Note: If you run “TRUNCATE” statement on a table, it cannot be revoked.

 

 

Syntax

The syntax of TRUNCATE statement in MySQL is as follows:

TRUNCATE TABLE [databaseName.]tableName;

 

How to delete all rows from a table in MySQL

Let’s take an example to see how it works. For this we will use the “Persons” table.

> SELECT * FROM Persons;

+----------+-----------+--------+-------------------------------+
| PersonID |    Name   |  age   |             Address           |
+----------+-----------+--------+-------------------------------+
|    101   |    Alex   |   25   | 819 Saint Francis Way         |
|    102   |   Emily   |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean    |   35   | 188 Clay Street Indiana       |
|    104   |    Bob    |   40   | 285 Java Lane Missouri        |
+----------+-----------+--------+-------------------------------+

Now, we are going to delete all rows from the “Persons” table.

> TRUNCATE TABLE Persons;
Query OK, 4 rows affected <0.13 sec>

Here is the result:

> SELECT * FROM Persons;
Empty set <0.02 sec>
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *