Difference between DELETE, TRUNCATE and DROP commands in SQL
DELETE
- DELETE is a Data Manipulation Language (DML) Command.
- We can use where condition for remove specific rows(records).
- DELETE is slower than TRUNCATE. it's maintain logs.
- Rollback is possible in Delete command.
- No triggers will be fired.
- Every row is locked for deletion in table using a row lock.
Syntax of a SQL DELETE Statement
//without where clause, removed all records
DELETE FROM table_name;
//with where clause, removed specific records
DELETE FROM table_name WHERE Condition;
TRUNCATE
- TRUNCATE is a Data Definition Language ( DDL) Command.
- TRUNCATE is used to remove all rows from the Table.
- Truncate is faster compared to delete, it does not maintain transaction log.
- Rollback is not possible (autocommit).
- No triggers will be fired.
- Truncate Table locks the table(entire table) and page.
Syntax of a SQL TRUNCATE Statement
//Removed all records
TRUNCATE TABLE table_name;
DROP
- DROP is a Data Definition Language( DDL) Command.
- DROP command removes all records,indexes,structure and privileges permanently.
- No DML triggers will be fired.
- Rollback is not possible.
Syntax of a SQL TRUNCATE Statement
DROP TABLE table_name;
What is difference between delete truncate and drop commands in mysql ?
|
DELETE |
TRUNCATE |
DROP |
Command Type |
DML |
DDL |
DDL |
Rollback Transactions |
Can be Rolledback |
No |
No |
Permanent Delete |
Does not remove record permanently |
Remove the record permanently |
Remove all records,indexes,structure and privileges permanently |
Trigger |
Trigger is fired |
No |
No |
Performance |
Slower than Truncate |
Faster than Delete |
Quick but some complications |
Can we use where clause |
Yes |
No |
No |
Syntax |
DELETE FROM table_name;
DELETE FROM table_name WHERE Condition; |
TRUNCATE TABLE table_name; |
DROP TABLE table_name; |
Related
find the nth highest salary in mysql without limit
How to find greater than average salary records in MySQL
how to find duplicate records in MySQL query
How to use second index forcefully in MySQL
How to get total number of rows in MySQL table with and without count