Thursday, May 19, 2022

DELETE and TRUNCATE commands

 

What is the difference between DELETE and TRUNCATE commands?

  • DELETE: This query is used to delete or remove one or more existing tables.
  • TRUNCATE: This statement deletes all the data from inside a table.

What is the difference between DELETE and TRUNCATE commands

The difference between DELETE and TRUNCATE commands are as follows:

  • TRUNCATE is a DDL command, and DELETE is a DML command.
  • With TRUNCATE, we cannot really execute and trigger, while with DELETE, we can accomplish a trigger.
  • If a table is referenced by foreign key constraints, then TRUNCATE will not work. So, if we have a foreign key, then we have to use the DELETE command.

The syntax for the DELETE command:

DELETE FROM table_name
[WHERE condition];

Example:

select * from stu

Output:
output 5

delete from stu where s_name=’Bob’

Output:

output 6

The syntax for the TRUNCATE command:

TRUNCATE TABLE
Table_name;

Example:

select * from stu1

Output:

output 7

truncate table stu1

Output:

output 8

This deletes all the records from a table.

No comments:

Post a Comment

Stored Procedure

  What is a Stored Procedure ? A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. S...