![]() |
SQL TRUNCATE TABLE command deletes all the records in a table while preserving the table structure. TRUNCATE In SQLTRUNCATE in SQL means deleting records of a table but preserving its structure like rows, columns, keys, etc. It is considered to be a DDL command instead of a DML command. SQL TRUNCATE TABLE is used for emptying a table while preserving table structure so that it can be re-populated.
SyntaxTRUNCATE TABLE Syntax is:
SQL TRUNCATE TABLE ExampleLet’s understand TRUNCATE in SQL with examples. Here we will look at different examples of the SQL TRUNCATE TABLE command. First, we will create a demo SQL database and table, on which we will use the TRUNCATE TABLE command.
The following table will be created. ![]() Records in the table TRUNCATE TABLE in SQL ExampleIn this example, we will Truncate the created table. Query: TRUNCATE TABLE EMPLOYEE; ![]() Truncating data After truncating data of our table, the data of our table has been erased but the structure is preserved so now if we perform SELECT * FROM EMPLOYEE command on our table we will see everything is erased and an empty set is being returned. ![]() No data is returned But let’s now check whether the structure of the table is deleted or it has been preserved so we again use the DESC command to see the structure of the table and we will see that the structure remains as it is. ![]() Structure is preserved SQL TRUNCATE vs DELETEUsing the TRUNCATE TABLE command is faster and consumes less memory than using the DELETE TABLE command. TRUNCATE TABLE is not used with WHERE Clause but DELETE TABLE command can be used with WHERE clause. SQL TRUNCATE vs DROPDROP TABLE command removes all records from the table, but it also deletes the structure of the table. The TRUNCATE TABLE command does not remove the table structure. TRUNCATE TABLE is a DDL (Data Definition Language) command, while DELETE is a DML (Data Manipulation Language) command. Important Points About SQL TRUNCATE TABLE
FAQs on SQL TruncateWhat is the use of the TRUNCATE command?
What is the difference between TRUNCATE AND DROP commands?
What is the difference between TRUNCATE AND DELETE command?
|
Reffered: https://www.geeksforgeeks.org
SQL |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |