Difference between DELETE, DROP and TRUNCATE

interview questions

In SQL, "DELETE", "DROP" and "TRUNCATE" are three different commands used to modify or delete data from a database. Here are the differences between them:

DELETE:

"DELETE" is a SQL command that is used to remove specific rows from a table. It is a Data Manipulation Language (DML) command that can be used with a "WHERE" clause to specify which rows to delete. When a row is deleted using DELETE, the data is removed from the table, but the table structure remains intact.

DROP:

"DROP" is a SQL command that is used to remove an entire table, view, or database from the server. It is a Data Definition Language (DDL) command that permanently deletes the table structure and all of its data. Once a table is dropped, it cannot be recovered.

TRUNCATE:

"TRUNCATE" is a SQL command that is used to remove all the data from a table quickly. It is a DDL command that removes all the data from the table but leaves the table structure intact. When a table is truncated, the storage space used by the table is released, and the table is reset to its original state.

Here are some key differences between DELETE, DROP and TRUNCATE:

  1. DELETE can remove specific rows from a table, while DROP and TRUNCATE remove the entire table.
  2. DELETE is a DML command, while DROP and TRUNCATE are DDL commands.
  3. DELETE can be rolled back using a transaction, while DROP and TRUNCATE cannot be rolled back.
  4. DELETE can trigger database triggers and logs the deletion activity, but DROP and TRUNCATE do not trigger any database triggers and cannot be logged.

It is important to use these commands with caution as they can have serious consequences on the data stored in a database. It is recommended to take a backup before performing these operations to avoid any loss of data.

Comments