Which statement is used to delete all rows in a table without having t...
TRUNCATE
The correct answer is option 'D' - TRUNCATE.
Explanation:
1. Introduction to TRUNCATE:
TRUNCATE is a SQL statement used to delete all rows from a table. It is a fast and efficient way of deleting the entire data from a table, as it removes all the rows without logging individual row deletions.
2. Comparison with DELETE:
The DELETE statement is also used to delete rows from a table, but it is different from TRUNCATE in several ways:
- DELETE is a Data Manipulation Language (DML) statement, whereas TRUNCATE is a Data Definition Language (DDL) statement.
- DELETE deletes rows one by one, whereas TRUNCATE removes all rows at once.
- DELETE operations are logged and can be rolled back, whereas TRUNCATE operations are not logged and cannot be rolled back.
3. Advantages of TRUNCATE:
TRUNCATE offers several advantages over DELETE when the goal is to remove all rows from a table:
- Faster performance: TRUNCATE is faster than DELETE, especially when dealing with large tables, as it does not generate individual row deletion logs.
- Resets identity columns: TRUNCATE also resets the identity columns to their initial seed values, whereas DELETE does not affect identity columns.
4. Syntax:
The syntax for using TRUNCATE is as follows:
TRUNCATE TABLE table_name;
5. Example:
For instance, let's consider a table called "employees" with several rows. To delete all the rows from this table using TRUNCATE, the following statement can be used:
TRUNCATE TABLE employees;
6. Confirmation prompt:
It is important to note that when TRUNCATE is used, it automatically removes all rows without any confirmation prompt. Therefore, it is crucial to double-check the table name before executing the TRUNCATE statement to avoid accidental data loss.
In conclusion, the TRUNCATE statement is used to delete all rows from a table without logging individual row deletions. It offers faster performance compared to DELETE and resets identity columns as well. However, caution should be exercised while using TRUNCATE to prevent accidental data loss.
Which statement is used to delete all rows in a table without having t...
TRUNCATE statement removes all rows in a table without logging the individual row deletions. It uses fewer system and transaction log resources, which makes its execution fast. This statement is similar to the DELETE statement without the WHERE clause.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).