Difference between Delete, Drop and Truncate in SQL
What is the Delete command in SQL?
In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL. We can say that Delete command is the part of Data Manipulation Language.
As we all know, the Delete command is used to delete records from the database table in SQL. With the help of the Delete command, we can delete the selected number of existing records from the database table in SQL.
We can also give a where clause commands to delete some specific rows or records from the database. The given condition decides which row should be deleted from the database. It helps us to maintain an entry in the transaction.
Syntax of Delete command in SQL:
DELETE FROM TableName WHERE condition;
Example:
Suppose we have a table named Employee. Now, in this table, we have multiple columns but we want to remove the data only from the employee whose employee_Id is 1011. So, we will use the where clause statement according to the given condition.
DELETE FROM Employee WHERE Employee_id = 1011;
Now, if we run this command then the only row will be deleted because of the where clause condition given by the user. It doesn’t affect the entire table in SQL.
How does the Delete statement work?
Let’s take an employee table to understand how delete command works in detail.
Employee name | Employee_id |
Rohit | 1011 |
Yashraj | 1012 |
Shubham | 1023 |
Varun | 1021 |
Akash | 1022 |
Now if we execute the given condition below it will only delete only single record from the table employee.
DELETE FROM Employee WHERE Employee_id = 1011;
After the execution of the above condition, the row where employee_id is 1011 will be removed from the table and the table will be as follows:
Output:
Employee name | Employee_id |
Yashraj | 1012 |
Shubham | 1023 |
Varun | 1021 |
Akash | 1022 |
As we can see, the delete command changes the table according to the given condition.
What is the Truncate command in SQL?
The truncate command is the part of the Data Definition Language. Using this command allows us to modify the databases in SQL by truncating some entries.
As we all know, the truncate command is used to delete all the records from the database table in SQL. With the help of the truncate command, we can delete all the existing data and rows from the database table in SQL.
In the truncate command, we don’t need to give the condition to delete the database, we just need the name of the table. It resets the identity of the table.
It is very fast compared to the delete command because it does not need a condition like delete. It deletes all the rows and data from the table. It can not be rolled back after using.
Syntax of Truncate command in SQL:
TRUNCATE TABLE table_name;
Example:
Suppose, we have a table named Employee. In this employee table, we have multiple rows and columns and the value is stored in the form of rows. Apply the following command to truncate the table.
TRUNCATE TABLE Employee;
Now, if we run this command then only the column name will remain and all the data from the table will be deleted.
How does the Truncate command work?
Let’s take an employee table to understand how truncate command works in detail. Truncate command changes the identity of the table.
Employee name | Employee_id |
Rohit | 1011 |
Yashraj | 1012 |
Shubham | 1023 |
Varun | 1021 |
Akash | 1022 |
Now, execute the following SQL query:
TRUNCATE TABLE Employee;
After the execution of this query, all the data from the table will be deleted except the structure of the table, and the table will not be deleted from the database but it will remain empty.
Employee name | Employee_id |
What is the Drop command in SQL?
Drop command is a DDL (Data Definition Language) command. We use the Drop command to modify the databases in SQL. With the help of the drop command, we can delete the entire table with all the data and also the structure of the table from the database in SQL.
Drop command is used to delete the entire existing table from the database. It can not be rolled back after we use this command.
Syntax of Drop command in SQL:
DROP TABLE table_name;
Example:
Suppose we have a table named Employee. In this employee table, we have multiple rows and columns, and the values are stored in the form of rows. Use the following SQL command to drop the table.
DROP TABLE table_name;
Now, if we execute the drop query, all the values from the table will be deleted with the structure of the table in the SQL database.