SQL Drop Table Statement
SQL is a programming language used to manipulate data that are stored in the database. It is flexible and user-friendly. In SQL, to interact with the database, the users have to type queries that have certain syntax, e.g., the drop command. The drop command in SQL is used to remove the whole table including data present inside it, indexes, triggers, constraints, and permission specifications for that particular table. It is basically a DDL(data definition language) which is irreversible in nature. It means that once the user drops a table, there is no way to undo the command. Therefore, the drop command must be used with extensive care. It can also delete a whole database or can simply delete a table, it depends on the wish of the user.
Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.
To drop table:
DROP TABLE table_name;
To drop database:
DROP DATABASE database_name;
Example 1: Drop the table
In this example, we will discuss how to drop a table named “categories” from a database named “Newcafe”.
Step 1: Create a database
CREATE DATABASE Newcafe;
Step 2: Use a database
Step 3: Create a table
CREATE TABLE [dbo].[categories] ( [CategoryID] INT NOT NULL PRIMARY KEY, [CategoryName] NVARCHAR(50) NOT NULL, [ItemDescription] NVARCHAR(50) NOT NULL ); GO
Add data inside a table
INSERT INTO [dbo].[categories] ( [CategoryID], [CategoryName], [ItemDescription] ) VALUES ( 1, 'Beverages', 'SoftDrink' ), ( 2, 'Condiments', 'Sweet and Savoury sauces' ), ( 3, 'Confections', 'Sweet bread' ) GO
View the final table:
SELECT * FROM categories;
Step 4: Drop the categories table
drop table categories;
Example 2: Drop the database
Now we drop the above create the database using the following command:
drop database Newcafe;