Câu hỏi phỏng vấn MySQL
Câu hỏi

What is the difference between TRUNCATE and DELETE ?

Câu trả lời

The difference between the TRUNCATE and DELETE commands in SQL revolves around their functionality, performance, and impact on database transactions and constraints.

  1. Functionality:

    • DELETE is used to remove specific rows from a table based on a condition provided by a WHERE clause. It can delete one or more rows depending on the condition specified. If no condition is provided, it can delete all rows in the table[1][2][3][4][5][6].
    • TRUNCATE, on the other hand, removes all rows from a table, effectively resetting the table to its initial state when it was empty. It does not allow for conditional deletion of rows[1][2][3][4][5][6].
  2. Performance:

    • DELETE operates row by row, logging each row deletion in the transaction log, which can make it slower, especially for large tables. It also maintains the integrity of the table and enforces constraints such as triggers and foreign keys during the deletion process[1][3][4][5][6].
    • TRUNCATE is faster because it deallocates the data pages used to store the table's data instead of removing rows one by one. This operation is minimally logged, which contributes to its speed. However, it does not enforce certain constraints like DELETE does[1][3][4][5][6].
  3. Transaction Log and Rollback:

    • DELETE operations are fully logged, meaning each deletion is recorded in the transaction log. This allows for the operation to be rolled back if necessary, making DELETE a safer option if there's a chance you might need to undo the operation[1][4][6].
    • TRUNCATE minimally logs its operation, recording only the deallocation of the data pages. This makes it impossible to roll back the operation using standard transaction control, which can be a significant drawback if an accidental truncation occurs[1][4][6].
  4. Resetting Identity Values:

    • DELETE does ...
junior

junior

Gợi ý câu hỏi phỏng vấn

junior

What is an AGGREGATE function. Name few aggregate functions used in MySQL.

expert

How do you make schema changes to a live database without downtime?

expert

What is the use of SAVEPOINT in MySQL?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào