In SQL Server, the DROP TABLE statement is used to remove or delete permanently from the database. In SQL Server after performing the DROP Operation we cannot revoke the table or database again, because it is an irreversible action. The Drop Operation is different from the DELETE Command, In the DELETE command we can revoke the database or table after performing the DELETE Operation. Generally, we use the DROP TABLE command to delete or remove those tables that are useless.
Syntax:
DROP TABLE [IF EXISTS] table_name;
Explanation:
- DROP TABLE: The keyword used to delete a table from the database.
- IF EXISTS: This optional clause prevents an error from occurring if the table does not exist and If the table exists then it is dropped otherwise, nothing will happen.
- table_name: It is a table name that we want to drop from our database.
If we want to delete multiple tables at the same time then the below syntax would be follow.
DROP TABLE IF EXISTS table_name1, table_name2, table_name3;
DROP a Table That Doesn't Exist
The following statement deletes the table named Geeksforgeeks.
DROP TABLE IF EXISTS Geeksforgeeks;
Explanation: In this example, this statement will delete the table named Geeksforgeeks, but if the table doesn't exist, then it will not raise any error. because we used the IF EXISTS clause that ensures that the statement doesn't fail in case the table doesn't exist in the database and the statement will be executed successfully.
The Result Looks Like:
After Command run
DROP a Single Table
Let's create a new table named Student.
CREATE TABLE Student (
StdtId INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Dob DATE
);
Now, to delete the table Student, we will write the following statement.
DROP TABLE Student;
Explanation: This statement will delete the Student table from the database. It will delete all the records stored in the Student table.
DROP a Table With FOREIGN KEY Constraint
Now, we will create two tables named Student and Course in the University schema. where the student table has a FOREIGN KEY constraint referencing the CourseId column in the Course table.
CREATE SCHEMA University;
GO
CREATE TABLE University. Course
(
CourseId INT PRIMARY KEY,
CourseName VARCHAR(100)
);
CREATE TABLE University. Student
(
StudentId INT PRIMARY KEY IDENTITY (1,1),
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Address NVARCHAR(255) NOT NULL,
CourseId INT NOT NULL,
FOREIGN KEY (CourseId) REFERENCES University.Course (CourseId)
);
Now Let’s try to drop the Course table from the University schema.
DROP TABLE University.Course;
The Result Looks Like:
After Command run
Explanation: In SQL ServeÂr, it is not allowed to delete a table that is referenced by foreÂign key constraints. If we want to remove this type of table, we have two options. either we have to delete foreign key constraints, or we have to delete referencing table first. In our example, we have to eÂither remove the foreÂign key constraints from the table or we have to remove the University.StudeÂnt table first before removing the University.Course table.
DROP TABLE University.Student;
DROP TABLE University.Course;
Explanation: In the above statement, we drop the referencing Student table first and then the Course table. So, it will be executed successfully.
The Result Looks Like:
After Command runConclusion
In conclusion, the DROP TABLE statement in SQL SeÂrver is a powerful command. It lets us fully erase a table with all its data from the database. ReÂmember we should always use this statement with caution, as we can't get a droppeÂd table back. Before executing the DROP TABLE statement, we must confirm that we have the needeÂd permissions. Also, we should check the existence of the table to prevent errors.
Similar Reads
SQL Server ALTER TABLE
In SQL Server, there are various commands to Add, Update, and Delete a Database Schema called DDL or Data Definition Language commands. A Table in a database is part of the database schema and the 'ALTER TABLE Moify Column' command is used to make changes to the database table column data type, colu
5 min read
SQL Server Describe Table
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting informati
4 min read
PostgreSQL - DROP TABLE
In PostgreSQL, the DROP TABLE statement is a powerful and permanent command used to delete one or more tables from a database. Since this operation cannot be undone, it is essential to understand how to use it safely and to be aware of its options to prevent accidental data loss. In this article, we
5 min read
SQL DROP TABLE
The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making
4 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read
SQL Server TRUNCATE TABLE
The TRUNCATE TABLE command in SQL Server allows you to rapidly and effectively remove every record from a table while maintaining the table structure. The TRUNCATE TABLE command is a more lightweight operation that is usually faster for large datasets than the DELETE statement, which eliminates rows
6 min read
SQL RENAME TABLE
Renaming a table is a common and useful operation for database administrators and developers. It is especially useful when we need to correct a naming mistake, organize our database schema, or update the table name to reflect new business requirements. In this article, we will provide a detailed gui
6 min read
Python PostgreSQL - Drop Table
In this article, we are going to see how to drop tables in PostgreSQL using pyscopg2 module Python. In PostgreSQL DROP TABLE is used to remove the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constraints for that table. If the p
2 min read
SQL Server DELETE and DROP TABLE Commands
In SQL Server, managing data involves not only inserting and updating records but also removing them when they're no longer needed. Two important commands for data removal are DELETE and DROP TABLE. These commands play crucial roles in maintaining database integrity and managing database structures.
4 min read