DROP TABLE
statement is used to delete the existing table from the database. There is another statement TRUNCATE TABLE
which is also used to delete the entire table. The difference between these two statements is that TRUNCATE TABLE
deletes all the rows from the table but table still exists in the database, however the DROP TABLE
statement deletes the table definition along with the data. That means table doesn’t exist in the database anymore after it is been dropped using DROP TABLE
statement.
Syntax:
DROP TABLE table_name;
Example: Let’s consider the same table that we have created in the CREATE TABLE tutorial.
Let’s check the existence of the table in the database first by using DESC statement:
DESC STUDENT;
This will show the table definition, which will confirm that the table STUDENT
in present in the database. We can now drop this table using DROP TABLE
statement.
DROP TABLE;
The above statement deleted the entire table from database, let’s check again using DESC command.
Query OK, 0 rows affected (0.01 sec)
This confirms that the table is deleted successfully from the database.
TRUNCATE TABLE:
We have learned above how to delete the existence of a table from database, however if you want table to exist in database but want to delete all its data (all the rows) then you can use TRUNCATE TABLE statement.
TRUNCATE TABLE table_name;
Leave a Reply