Delete all records from a table in MySQL?

MySQLMySQLi Database

To delete all the records from a table in MySQL, use the TRUNCATE command. Let us fir see the syntax −

TRUNCATE TABLE yourTableName.

The above syntax will delete all the records from a table. Let us create a table to understand the above syntax −

mysql> create table TruncateTableDemo
−> (
   −> BookId int
   −> ,
   −> BookName varchar(200)
−> );
Query OK, 0 rows affected (0.54 sec)

Inserting records in the table with the help of insert command. The query to insert records in the table are as follows −

mysql> insert into TruncateTableDemo values(1001,'C in Dept');
Query OK, 1 row affected (0.18 sec)

mysql> insert into TruncateTableDemo values(1002,'The C++ Programming');
Query OK, 1 row affected (0.22 sec)

mysql> insert into TruncateTableDemo values(1003,'Let us C');
Query OK, 1 row affected (0.16 sec)

Now you can display all the records from the table with the help of a select statement. The query is as follows −

mysql> select *from TruncateTableDemo;

The following is the output −

+--------+---------------------+
| BookId |           BookName  |
+--------+---------------------+
| 1001   |           C in Dept |
| 1002   | The C++ Programming |
| 1003   |            Let us C |
+--------+---------------------+
3 rows in set (0.00 sec)

Implement the above syntax to delete all the records from the table. The query is as follows −

mysql> truncate table TruncateTableDemo;
Query OK, 0 rows affected (0.93 sec)

Now you can check all records have been deleted from the table or not.

The following is the query −

mysql> select *from TruncateTableDemo;
Empty set (0.00 sec)
raja
Published on 10-Jan-2019 11:21:51
Advertisements