Introduction
DROP and DELETE Table in SQL are the commands to remove the table with all its data and delete the data of that specific table, respectively.
So let’s learn about Drop and Delete statements one by one.
DROP TABLE
DROP table query in SQL removes one or more table definitions and all the data, indexes, triggers, constraints, and permission specifications for those tables in the database.
It is a DDL( Data Definition Language) command. We cannot roll back this operation, i.e., once the Drop statement is used, we can not recover data of that table from the database.
DELETE TABLE
A DELETE query in SQL can delete all the data from the table in the database. We can use the WHERE clause with the DELETE command to delete specific records from the table.
It is a DML(Data manipulation language ) command. The DELETE command when used with the WHERE clause deletes one row at a time and records the entry in the transaction log for each deleted row of the table. To use the Delete command, we need DELETE permission on the table.
NOTE: In some places, Rows are referred to as Tuples.
Recommended topics, Coalesce in SQL and Tcl Commands in SQL
Syntax
Syntax of Drop and Delete command are as follows
DROP Command:
DROP TABLE table_name; |
DELETE Command:
DELETE FROM table_name WHERE condition; |
Example
Let’s take an example that shows how to use these Drop and Delete commands each.
DROP Command:
Suppose there is a table named customers in the database, and we want to delete this table completely. So, we will use the given command.
Given below is the Customer table in a database named “Customers”.
CustomerID | CustomerName | City | Contact | PinCode | Country |
1 |
Nayan Prakash | Mumbai | 7256984661 | 402086 | India |
2 |
Akash Dixit | Noida | 9965485234 | 210451 | India |
3 |
Anjali Singh | Gurgaon | 9886542498 | 304056 | India |
4 |
Ayush Mishra | Delhi | 7554655646 | 206206 | India |
5 |
Raman Goyal | Kanpur | 7412336954 | 208002 | India |
The following SQL statement will also completely delete all the table and table definitions records.
DROP TABLE customers; |
Now, if we try to access this table, we will get the following error
ERROR 1146 (42S02): Table ‘Customers’ does not exist. |
Thus, all the records associated with the Customers table will be deleted as well as its table definition will be deleted from the database.
DELETE Command:
Let's say below is the “Customers” table in a database.
CustomerID | CustomerName | City | Contact | PinCode | Country |
1 |
Nayan Prakash | Mumbai | 7256984661 | 402086 | India |
2 |
Akash Dixit | Noida | 9965485234 | 210451 | India |
3 |
Anjali Singh | Gurgaon | 9886542498 | 304056 | India |
4 |
Ayush Mishra | Delhi | 7554655646 | 206206 | India |
5 |
Raman Goyal | Kanpur | 7412336954 | 208002 | India |
The following SQL statement will delete the customer “Akash Dixit” from the “Customers” table.
DELETE FROM Customers WHERE CustomerName=’Akash Dixit’ ; |
Now the table will look like this:
CustomerID | CustomerName | City | Contact | PinCode | Country |
1 |
Nayan Prakash | Mumbai | 7256984661 | 402086 | India |
3 |
Anjali Singh | Gurgaon | 9886542498 | 304056 | India |
4 |
Ayush Mishra | Delhi | 7554655646 | 206206 | India |
5 |
Raman Goyal | Kanpur | 7412336954 | 208002 | India |
Must Read, sql functions and SQL EXCEPT