Introduction

This is part four of "Zero To Hero In MS SQL Server", and in this article, you will learn about dropping, deleting, and truncating tables in MS SQL Server. To read the previous articles under this series, please follow the below-mentioned links,

The following are the topics of this article,

A Glimpse at the previous articles.

In Part One,

In Part Two,

In Part Three,

To perform dropping, deleting, and truncating a table, I am creating three tables. With these three tables let's look into more

Drop Table

The syntax for dropping a table in MS SQL is,

Syntax

DROP TABLE tablename;

Example

DROP TABLE Students.StudentClass

When the above statement is executed, it will remove the table from the database. When a table is dropped from the database, the user cannot roll back the table.

DROP statement is a DDL command.

When again the user tries to select values from this dropped table, the system will show an error message as "Invalid object name 'Students.StudentClass.

Delete Table

The Syntax for deleting a table is,

Syntax

DELETE FROM TABLE WHERE CONDITION...

Example

DELETE FROM Students.StudentMarks WHERE Grade='F'

When the above statement is executed, it will delete data from the table where the Grade is.

A DELETE statement is used to delete a particular data from the table. Like the drop statement, here the entire table is not removed. Only the particular data is removed from the table. DELETE statement is a DML command. We need to use the WHERE clause to delete from the table.

Truncate Table

The syntax for Truncating a table is,

Syntax

TRUNCATE TABLE tableName

Example

TRUNCATE TABLE Students.StudentDetails

When the above statement is executed, it will remove all the data in the table StudentDetails

TRUNCATE is a DDL command. When the truncate statement is used for a table, the entire data in the table will be deleted, i.e., remove all rows from the table.

Points To Remember

Conclusion

In this article, we have learned about dropping a table, deleting a table, and truncating a table. I hope this was useful and in my next article, we will learn more details. Please share your feedback in the comment section.