SQL Truncate, Drop or Rename a Table
In this tutorial we will learn about the various DDL commands which are used to re-define the tables.
TRUNCATE
command
TRUNCATE
command removes all the records from a table. But this command will not destroy the table's structure. When we use TRUNCATE
command on a table its (auto-increment) primary key is also initialized. Following is its syntax,
TRUNCATE TABLE table_name
Here is an example explaining it,
TRUNCATE TABLE student;
The above query will delete all the records from the table student.
In DML commands, we will study about the DELETE
command which is also more or less same as the TRUNCATE
command. We will also learn about the difference between the two in that tutorial.
DROP
command
DROP
command completely removes a table from the database. This command will also destroy the table structure and the data stored in it. Following is its syntax,
DROP TABLE table_name
Here is an example explaining it,
DROP TABLE student;
The above query will delete the Student table completely. It can also be used on Databases, to delete the complete database. For example, to drop a database,
DROP DATABASE Test;
The above query will drop the database with name Test from the system.
RENAME
query
RENAME
command is used to set a new name for any existing table. Following is the syntax,
RENAME TABLE old_table_name to new_table_name
Here is an example explaining it.
RENAME TABLE student to students_info;
The above query will rename the table student to students_info.