Tuesday, 21 January 2025

DROP, DELETE, and TRUNCATE

Here’s a comparison of DROP, DELETE, and TRUNCATE in SQL:


Aspect DROP DELETE TRUNCATE
Purpose Removes an entire table or database. Removes specific rows from a table. Removes all rows from a table.
DML/DDL DDL (Data Definition Language). DML (Data Manipulation Language). DDL (Data Definition Language).
Deletes Data Only? Deletes both structure and data. Deletes data, keeps table structure. Deletes data, keeps table structure.
WHERE Clause Not applicable. Can use WHERE to filter rows. Cannot use WHERE.
Performance Faster for dropping structure. Slower for large data (logged). Faster (minimal logging).
Rollback Support Cannot be rolled back. Can be rolled back if in a transaction. Cannot be rolled back.
Resets Identity? Yes, as table is removed. No, retains identity sequence. Yes, resets identity sequence.

Examples:

  1. DROP:

    DROP TABLE employees;
    
  2. DELETE:

    DELETE FROM employees WHERE department_id = 101;
    
  3. TRUNCATE:

    TRUNCATE TABLE employees;
    


No comments:

Post a Comment

how to create dml dynamically in Ab-initio

 $[ begin let int i = 0; let string(int) complete_list = "emp_nm,billable,age,designation,location"; let string(int) file_content ...