
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Does MySQL DROP TABLE completely remove the table or just the structure?
The DROP TABLE removes the table completely and also removes all data. If you want to remove all data completely and wants the table structure, then you can use TRUNCATE TABLE command. The TRUNCATE command will recreate the table.
Let us first check the DROP TABLE. For that, we will first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (0.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.05 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Name | +----+-------+ | 1 | Larry | | 2 | Chris | +----+-------+ 2 rows in set (0.00 sec)
Now, use DROP TABLE command. After executing the DROP TABLE command, the data will be deleted as well as there won’t be any table structure.
mysql> DROP TABLE DemoTable; Query OK, 0 rows affected (0.18 sec)
If you try to display data, you will get the following error −
mysql> select *from DemoTable; ERROR 1146 (42S02): Table 'hb_student_tracker.DemoTable' doesn't exist
You won’t even get the table structure −
mysql> desc DemoTable; ERROR 1146 (42S02): Table 'hb_student_tracker.DemoTable' doesn't exist
Let us implement the TRUNCATE command.
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | +----+-----------+ 2 rows in set (0.00 sec)
Now, implement TRUNCATE command. After executing the TRUNCATE command, data will be removed from the table and table structure will be recreated.
mysql> TRUNCATE TABLE DemoTable; Query OK, 0 rows affected (0.06 sec)
Now you won’t get any data from table but the table structure would be visible −
mysql> select *from DemoTable; Empty set (0.00 sec)
Following is the query to get table structure −
mysql> desc DemoTable;
This will produce the following output −
+-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | FirstName | varchar(20) | YES | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
- Related Articles
- What MySQL returns when we remove all the columns from a table by using ALTER TABLE command with DROP keyword?
- Fix Drop table view #1051 unknown table error in MySQL
- How can we automatically define the structure of MySQL table same as the structure of another table?
- How to Drop MySQL Table using Java?
- Drop a MySQL Table after x hours?
- Remove unique key from MySQL table?
- Remove index from a MySQL table
- Does MySQL support table inheritance?
- How do I clone the structure of a table in MySQL?
- MySQL query to display structure of a table
- Determining table or structure of an ABAP code
- How can we drop UNIQUE constraint from a MySQL table?
- How can we get the structure of a MySQL view as we can get the structure of a MySQL table?
- Remove only the percentage sign from values in a MySQL table?
- Fetch fields from table or structure in ABAP SAP
