SQL - SPLessons

SQL Drop Table

Chapter 14

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Drop Table

SQL Drop Table

shape Description

SQL Drop Table command can be performed on existing database. Indexes, tables, and databases can easily be erased/evacuated with the DROP statement. The following are the actions performed by drop command.

  • Dropping a section from the table.
  • Dropping a table from the database.

Dropping a column from the table

shape Syntax

Alter table drop column;

Table_name => Any accurate table.

Column_name =>The operation that  can be performed on a column in the table.

shape Examples

By viewing the below example, the concept of drop command can be understood easily.

sql> desc student;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
| fee        | int(11)      | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

sql> alter table student drop column fee;
Query OK, 0 rows affected (0.53 sec)
Records: 0  Duplicates: 0  Warnings: 0

sql> desc student;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

The above example tells that, how to drop a column_name fee from the table.

Dropping a table from the database

shape Syntax

drop table <table name>;

shape Examples

By viewing the below example, the concept of SQL Drop Table command can be understood easily.

sql> desc student;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

sql>drop table student;
Query OK, 0 rows affected (0.48 sec)

sql> select * from student;
ERROR 1146 (42S02): Table 'employee.student' doesn't exist

The above example tells that, how to drop all the columns from the table student.

SQL Drop Table is used to drop table from a database. It removes the table from the database schema, there is no way to recover the data after execution of this statement on the table.

Summary

shape Key Points

  • SQL Drop Table – SQL Drop Table will drop a column from the table and drop table will drop the entire table.