Delete and Truncate Query in SQL

The Delete query in SQL only deletes records from the table, and does not allow any description changes, i.e. it just manipulates them. Therefore, it is DML (Data Language Manipulation). The Truncate command in SQL eliminates all rows from a table, i.e. by making changes to the data description, it reinitializes identity. That’s why it’s a DDL (the Data Description Language).

Let’s debate it.

Delete Command in SQL

Essentially the Remove query in SQL is used to remove or erase one or more current records from a list. It is used along with clause Where. Though, in the Delete question the Where condition is optional.

How to Delete Table in SQL

Syntax:

DELETE FROM table_name
[WHERE condition];

Where the keywords DELETE, FROM, and WHERE are and the table name is the table name. And in the section on condition, a condition is given and a semicolon followed.

How to Delete Row in SQL

Let’s use the Delete command in SQL and delete the record of an employee whose age is 33 years.

Example:

DELETE FROM employee
WHERE e_age = 33;
Delete Command in SQL

Please do not, because only one record is affected here

After the question is registered, click the execute button to search for errors

Upon execution of the application, a message will appear as ‘Commands completed successfully’

How to Delete Column in SQL

Let’s delete a record with a condition where the name of the employee is sam

DELETE FROM employee
WHERE e_name = ‘sam’;
Delete Command in SQL

Just one record is affected here too

After the question is registered, click the execute button to search for errors

Upon execution of the application, a message will appear as ‘Commands completed successfully’

Let’s test the actualization

SELECT * from employee;

Delete Command in SQL

Tap on the Run button to check for errors after writing the question

Upon execution of the application, a message will appear as ‘Commands completed successfully’

Truncate Command in SQL

For SQL, the Truncate deletes all the data inside a row. This command removes all table records but retains table structure

TRUNCATE TABLE table_name;

where TRUNCATE TABLE is the keyword and table_name is the name of the table, followed by a semicolon.

Let’s remove all the records of the employee table using the Truncate command in SQL

TRUNCATE TABLE employee;
Truncate Command in SQL

After the question is registered, click the execute button to search for errors

Upon execution of the application, a message will appear as ‘Commands completed successfully’

Let’s check if the employee table structure is conserved or not

SELECT * FROM employee

Truncate Command in SQL

We can see that the structure is still intact

After the question is registered, click the execute button to search for errors

Upon execution of the application, a message will appear as ‘Commands completed successfully’

SQL Delete vs SQL Truncate

Ok, this is how we function in SQL with Delete query, and where to use Truncate in SQL order. The following table gives a short difference between those two commands.

Difference in Delete And Truncate

It takes us to the end of this tutorial section where we learned how to delete and truncate SQL commands. Here we learned how to delete SQL column, how to delete SQL lines, and how to delete SQL table, and how to use SQL truncate table.

Leave a Reply