Categories: DBMS

SQL: Truncate, Drop, Rename Query

The various DDL commands that are used to redefine the tables will be covered in this tutorial.

Truncate Query:

TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the extent of a table for deallocation (empty for reuse). This method instantly deletes all of the data from a table while often evading several integrity-checking techniques. It was introduced formally in the SQL:2008 standard. The DELETE FROM mytable statement and the TRUNCATE TABLE mytable statement are similar theoretically but not physically (without a WHERE clause).

Syntax:

TRUNCATE TABLE  table_name;

Example:

Student_IDStudent_NameStudent_CourseStudent_Marks
1AnujB.tech88
2RamanMCA98
3ShyamBBA92
Student

Query to truncate the table:

TRUNCATE TABLE  Student;

The above query will delete all the records from the table Student.

Output:

Student_IDStudent_NameStudent_CourseStudent_Marks
Student
Drop Query:

The drop command is used to completely remove a table from a database and erases the table’s structure. The drop command deletes all of the table’s indexes, constraints, triggers, and other definitional elements. When the DROP command is performed, the data kept in the database is also gone and cannot be restored.

Syntax:

DROP TABLE table_name;  

Example:

This query deletes the created Student table from the database:

DROP TABLE Student;  
Rename Query:

The RENAME command in a database can be used to rename a table. The syntax for the rename command is as follows:

RENAME TABLE old_table_name to new_table_name

Example:

Query to rename the Student table to Student_Details.

RENAME TABLE Student to Student_Details

Output:

Student_IDStudent_NameStudent_CourseStudent_Marks
Student_Details

Note: also read about SQL: ALTER command

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago