In this topic, we described about the below sections -
Why to truncate a table?
Table consists of data in the form of rows and columns. The data is inserted to the table in various ways. The reasons behind of deleting the table data are –
- Data might insert wrongly
- Data might have loaded incorrectly.
- The data storage requirement has fulfilled (like pending bills, pending transactions, pending payments etc,.) and many more.
DELETE FROM statement fulfils the requirement of deleting the data. Apart from this DELETE FROM statement, TRUNCATE TABLE statement also used to delete the data from the table.
How to use TRUNCATE TABLE?
TRUNCATE TABLE is used to delete all the information in existing table, but structure of the table will remain same. By using TRUNCATE TABLE Statement, we cannot delete specified rows of data in table and WHERE clause does not exists in TRUNCATE TABLE Statement.
TRUNCATE TABLE table_name;
- table_name – Represents name of the table which is used to truncate.
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
In the below example, we are trying to delete rows from employee_details table. The query for the same is -
TRUNCATE TABLE employee_details;
If the above query executed successfully, the data from employee_details table gets deleted. However, the employee_details table existing in the database.
Once the TRUNCATE TABLE statement executed successfully, then we can conclude that the table data deleted successfully. However, it is always best practice to verify once before confirming to the external world.
To verify the table data gets deleted or not, just trigger a select query on the table like below -
SELECT * FROM employee_details;
If it displays empty table like below, then it confirms that the table data got deleted successfully.