In this MySQL tutorial, you will learn everything about the MySQL TRUNCATE TABLE statement to delete all the data from the table but the table remains the same.
MySQL TRUNCATE TABLE Introduction
Sometimes we want to delete all the from the MySQL table but the does not. In that case, we can use the MySQL TRUNCATE TABLE statement.MySQL TRUNCATE TABLE command capable for delete all the records from the table.
The basic syntax of MySQL TRUNCATE TABLE statement is:-
TRUNCATE [TABLE] tbl_name;
Let’s break down the above syntax:
- TRUNCATE is keyword that is used to delete all the data from the table.
- TABLE is an optional, so you can avoid it.
- tbl_name represent the table name which you want to use.
MySQL TRUNCATE TABLE Example
To use the MySQL TRUNCATE command, first, we will create a sample table and insert some records.
Example:- Create a table in MySQL
CREATE TABLE students ( st_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, roll_no INT, UNIQUE(roll_no) );
Example:- Insert records in MySQL table
INSERT INTO students (st_id, name, roll_no) VALUES (1,'Vishvajit', 100), (2, 'John', 101), (3, 'Shivam', 102);
You can use SELECT STATEMENT to see all inserted records in the table.
Example: Select all records from the MySQL table
SELECT * FROM students;
+-------+-----------+---------+ | st_id | name | roll_no | +-------+-----------+---------+ | 1 | Vishvajit | 100 | | 2 | John | 101 | | 3 | Shivam | 102 | +-------+-----------+---------+ 3 rows in set (0.03 sec)
Now, it’s time to delete all the data or records from the students but the table remains the same.
Example: Truncate table in MySQL
TRUNCATE TABLE students;
Again, When you will use the SELECT STATEMENT, you will not see any result.
So, here we have been seen all about the MySQL TRUNCATE TABLE statement to delete all the data from the MySQL database table. This is one of the very useful commands, Especially when you are going to delete all the records from the
students table by you want the to table remain the same.
I hope this article will help you. If you like this article, please share, support, and keep visiting for further MySQL tutorials.
Reference:- Click Here
Thanks for your valuable time …