This is one of the common SQL interview questions. Moreover, these commands are used more often in our day to day work. So let us go ahead and learn the purpose of each command. Hopefully, this short article will be beneficial for you.
Before we understand the commands, there is an important concept which we need to understand first:
What is Data and Schema in SQL Table?
A table is created using the Create command and it specifies the structure / skeleton of the table. It specifies the name of the table, no. of columns a table will have, the data type of each column etc. Whereas the Data is the actual values which get inserted as a row for each of the table columns.
Let us understand the main question now:
1) Delete:
Definition: Delete command is used to delete existing records / rows from the table.
Syntax: Delete From SchemaName.TableName where ColumnName = filterValue;
Important Points:
2) Truncate:
Definition: Truncate command is used to delete all the records / rows from the table. In other words, It empties the table.
Syntax: Truncate Table SchemaName.TableName;
Important Points:
3) Drop:
Definition: Drop command is used to delete the table schema along with data.
Syntax: Drop Table SchemaName.TableName;
Important Points:
Delete and Truncate, which one to use when?
We collect cookies and may share with 3rd party vendors for analytics, advertising and to enhance your experience. You can read more about our cookie policy by clicking on the 'Learn More' Button. By Clicking 'Accept', you agree to use our cookie technology.
Our Privacy policy can be found by clicking here