TRUNCATE TABLE statement in SQL

The TRUNCATE TABLE statement is used to completely delete records from an existing table in SQL.

The TRUNCATE TABLE statement is used to completely delete records from an existing table in SQL.

This command works similar to DROP TABLE to delete the entire table, but TRUNCATE will delete the entire table structure from the Database and if you need to store some data with such a table structure, you will have to re-create the table again. .

The syntax for using TRUNCATE TABLE in SQL

The basic syntax of the TRUNCATE TABLE command to remove records from the table is as follows:

 TRUNCATE TABLE ten_bang; 

Example of TRUNCATE TABLE in SQL

Suppose the NHANVIEN table has the following records:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

Execute the Truncate statement as follows:

 SQL > TRUNCATE TABLE NHANVIEN; 

Now, the NHANVIEN table has been removed from the records and the results from the SELECT statement returned are:

 SQL> SELECT * FROM NHANVIEN; 
Empty set (0.00 sec)

In the next section, we will learn how to use VIEW in SQL , please remember to follow.

Previous article: Index (INDEX) in SQL

Next lesson: The view VIEW framework in SQL

4 ★ | 1 Vote