T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - DROP TABLE



The T-SQL (Transact-SQL) DROP TABLE statement is used to delete a table from the database. It drops all the data, indexes, triggers, constraints and permission specifications for the specified table.

Note: Be careful before dropping a table. Once deleted, all information stored in that table will be lost forever!.

Syntax

The syntax of using DROP TABLE statement in T-SQL (Transact-SQL) is given below:

DROP TABLE [IF EXISTS] table_name;

The IF EXISTS is an optional parameter that conditionally drops table only if it exists on the database. If a table is deleted which does not exist, it will raise an error.

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The table can be checked using EXEC sp_help command as shown below:

EXEC sp_help Employee;

This will produce the result as shown below:

NameOwnerTypeCreated_datetime
Employeedbouser table2019-10-21 10:18:45.477

To delete this table, the query is given below:

DROP TABLE Employee;

After dropping the table, the EXEC sp_help command will throw following error:

EXEC sp_help Employee;	
Result: The object 'Employee' does not exist in database 'testDB' or is invalid for this operation.

Here, testDB is the name of database which initially had Employee table.