How do you drop a table in SQL?
The SQL DROP TABLE statement is used to remove a table definition and all the data, indexes, triggers, constraints and permission specifications for that table
The SQL Server (Transact-SQL) DROP TABLE statement allows you to remove or delete a table from the SQL Server database.
This is very important to know that once a table is deleted all the information available in the table is lost forever, so we have to be very careful when using this command.
DROP table syntax:
DROP table output:
DROP
- First, specify the name of the table to be removed.
- Second, specify the name of the database in which the table was created and the name of the schema to which the table belongs. The database name is optional. If you skip it, the DROP TABLE statement will drop the table in the currently connected database.
- Third, use IF EXISTS clause to remove the table only if it exists. The IF EXISTS clause has been supported since SQL Server 2016 13.x. If you remove a table that does not exist, you will get an error. The IF EXISTS clause conditionally removes the table if it already exists.
Note:
Be careful before dropping a table. Deleting a table will result in loss of complete information stored in the table!
DROP TABLE and CREATE TABLE should not be executed on the same table in the same batch. Otherwise an unexpected error may occur.
It is sometimes easier to drop a table and recreate it, instead of using the ALTER TABLE statement to change the table's definition
No comments:
Post a Comment