SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
Difference Between DROP and Truncate
In this article, we will learn about the DROP and Truncate command and the differences between them.
What is DROP Command?
DROP is a command of Data Definition Language which removes or deletes the definition indexes, data, constraints, and triggers from the database tables.
In the Structured Query Language, this statement deletes the elements from the relational database management system (RDBMS).
The DROP command is fast in comparison to the TRUNCATE command in SQL. But, its execution time is slow because it has many complications.
We can also use the DROP keyword with the ALTER TABLE statement for deleting one or more columns from the tables.
The syntax for deleting the database is given below:
In this syntax, we have to specify the name of the database which we want to delete from the system.
The syntax for deleting the table is given below:
In this syntax, we have to specify the name of the table which we want to delete from the database.
The syntax for deleting the multiple tables in one statement is given below:
In this syntax, we can specify multiple tables using a comma.
The syntax for deleting the index is given below:
In this syntax, we have to specify the name of the index just after the INDEX keyword.
Unlike the DELETE command in SQL, database users cannot roll back the data from the table after using the DROP command.
The DROP command frees the tablespace from the storage because it permanently deletes the table and its content from the system.
Example of DROP Command
Let's create a new table for implementing the DROP query in SQL. The following CREATE TABLE query creates the Employee table with five fields:
(
Employee_ID INT AUTO_INCREMENT PRIMARY KEY,
Emp_Name VARCHAR (50),
Emp_Salary INT NOT NULL,
Emp_CityVarchar (50),
Emp_EmailID VARCHAR (100)
) ;
The following query deletes the created Employee table from the database:
What is TRUNCATE Command?
TRUNCATE is also another command of Data Definition Language. This command removes all the values from the table permanently.
This command cannot delete the particular record because it is not used with the WHERE clause.
Syntax of TRUNCATE command in SQL:
In the above syntax, we have to specify the name of that table whose all records we want to delete from the table.
Example of TRUNCATECommand
Let's create a new table for implementing the TRUNCATE query in SQL. The following CREATE TABLE statement creates the Student_Details table with five columns:
(
Student_ID INT NOT NULL,
Student_Name varchar(100),
Student_Course varchar(50),
Student_Age INT,
Student_Marks INT
);
The following SQL queries insert the record of students into the above table using INSERT INTO statement:
INSERT INTO Student_Details VALUES (102, Raman, MCA, 24, 98);
INSERT INTO Student_Details VALUES (104, Shyam, BBA, 19, 92);
INSERT INTO Student_Details VALUES (107, Vikash, B.tech, 20, 78);
INSERT INTO Student_Details VALUES (111, Monu, MBA, 21, 65);
INSERT INTO Student_Details VALUES (114, Jones, B.tech, 18, 93);
INSERT INTO Student_Details VALUES (121, Parul, BCA, 20, 97);
INSERT INTO Student_Details VALUES (123, Divya, B.tech, 21, 89);
INSERT INTO Student_Details VALUES (128, Hemant, MBA, 23, 90);
INSERT INTO Student_Details VALUES (130, Nidhi, BBA, 20, 88);
Let's see the record of the above table using the following SELECT statement:
Student_ID | Student_Name | Student_Course | Student_Age | Student_Marks |
---|---|---|---|---|
101 | Anuj | B.tech | 20 | 88 |
102 | Raman | MCA | 24 | 98 |
104 | Shyam | BBA | 19 | 92 |
107 | Vikash | B.tech | 20 | 78 |
111 | Monu | MBA | 21 | 65 |
114 | Jones | B.tech | 18 | 93 |
121 | Parul | BCA | 20 | 97 |
123 | Divya | B.tech | 21 | 89 |
128 | Hemant | MBA | 23 | 90 |
130 | Nidhi | BBA | 20 | 88 |
The following query removes all the rows from the above Student_Details table:
The following statement verifies that the table is successfully truncated or not:
Output:
Student_ID | Student_Name | Student_Course | Student_Age | Student_Marks |
---|---|---|---|---|
The following table shows the differences between DROP and TRUNCATE command in Structured Query Language:
DROP | TRUNCATE |
---|---|
The DROP command in SQL removes the table definition and its data. | The TRUNCATE command in SQL deletes all data from the table. |
This query frees the tablespace from the memory. | The TRUNCATE query does not free the tablespace from the storage. |
The view of the table does not exist in the DROP command. | View of the table exists in the Truncate command. |
The integrity constraints will be automatically removed from the table in the DROP command | The integrity constraints in this command will not be removed. |
In the DROP query, deleted space is not used. | The deleted space is used but less than the DELETE statement. |
The DROP query deletes data quickly, but there are so many complications. | The TRUNCATE query in SQL is faster than the DROP query. |