So sánh drop delete và truncate năm 2024

DELETE command is a DML (Data Manipulation Language) command that is used to delete existing records from the table.With the delete command users have to give a condition with where clause and that given condition decides which row/records will be deleted.

It can delete one or more rows/records of the table.The DELETE statement does not delete the table structure, It just deletes the records present inside it.

Syntax

DELETE FROM table_name WHERE condition;

Note: If the user omits the WHERE clause, all records in the table will be deleted.

Example

DELETE FROM Employee WHERE EmpID=15;

DROP command-

DROP command is a DDL( Data Definition Language) command that is used to drop the complete table from the database.DROP Command removes the table structure as definition of the table, all the data, indexes, triggers, constraints, and permission specifications of that table.

Syntax

DROP TABLE table_name;

NOTE-With the Drop command, we don’t have to give any condition with a where clause. We only require the table name.

Example–

DROP Table Employee;

If we run the above DROP query then the entire table will be removed from the database.

DROP DATABASE Company;

This query will delete the database Company.

TRUNCATE Command

TRUNCATE command is a DDL( Data Definition Language) command that is used to delete the whole data from a table but not the table itself .

With the Truncate command, we don’t have to give any condition with a where clause. We only require the table name.

Syntax

TRUNCATE TABLE table_name;

Example

TRUNCATE TABLE Employee;

Difference between DELETE and TRUNCATE

  1. The DELETE command is used to delete a particular row/records(one or more) from the table, while the TRUNCATE command will delete entire rows from a table.
  2. The DELETE command may have a WHERE clause in order to filter the records,while the TRUNCATE command does not have a WHERE clause.
  3. DELETE operation operates on data records and executes deletion one-by-one in the order of the queries processed.whereas TRUNCATE operates on data pages and executes deletion of the whole table data at a time.
  4. DELETE removes row-by-row one at a time from the table. However, TRUNCATE deletes entire table data at a time, that’s why TRUNCATE is faster than DELETE.

Difference between DROP and TRUNCATE

  1. The DROP command is used to remove table structure and its contents.Whereas the TRUNCATE command is used to delete all the rows/records from the table,it will not remove the table structure.
  2. The DROP command removes the space allocated for the table from memory.The TRUNCATE command does not free the space allocated for the table from memory.
  3. In the DROP command the Integrity Constraints get removed f.Whereas in TRUNCATE command the Integrity Constraints will not get removed. The DROP Command has slower performance than TRUNCATE because it deletes the table from the database after deleting the rows.The TRUNCATE command works faster than the DROP command because it deletes all the records from the table without any condition.

Quản Trị Mạng - Trong bài viết dưới đây, chúng tôi sẽ giúp các bạn phân biệt một số điểm khác biệt cơ bản giữa 2 cú pháp DELETE và TRUNCATE TABLE trong ứng dụng Microsoft SQL Server. Về mặt cơ bản thì cả 2 câu lệnh này đều giúp chúng ta xóa bỏ dữ liệu, nhưng về bản chất thì lại không phải như vậy.

Câu lệnh DELETE

Lệnh này sẽ giúp chúng ta xóa bỏ các bản ghi – Record khỏi cơ sở dữ liệu theo hàng – Row. Khi 1 bản ghi như vậy được xóa bằng DELETE, toàn bộ thành phần bên trong đều được ghi lại trong phần Transaction Log, các phần ràng buộc được kiểm tra, và bất cứ thành phần nào thừa sẽ bị xóa hoàn toàn. Bên cạnh đó, những bản ghi nếu vô tình bị xóa bằng lệnh DELETE này thì vẫn có thể được phục hồi được. Trong khi DELETE thường được dùng để xóa dữ liệu bản ghi khỏi bảng nhiều hơn khi áp dụng trong một phần dữ liệu nào đó, bên cạnh đó, câu lệnh này thường được dùng đối với những bảng dữ liệu đơn, hoặc trong trường hợp muốn xóa dữ liệu khỏi 1 bảng nào đó khi cần kết hợp với nhiều bảng có quan hệ khác. Mặt khác, việc phân quyền chức năng DELETE trên bảng chỉ được áp dụng trên những tài khoản người dùng khác nhau mà không cần phải gán ownership trên tài khoản đó. Và lệnh DELETE không thay đổi việc tự động tăng hoặc giảm số lượng cột – Column trong bảng chứa dữ liệu đó.

Cú pháp chung:

[ WITH [ ,...n ] ] DELETE

 [ TOP ( expression ) [ PERCENT ] ]  
 [ FROM ]   
    { table_name [ WITH (  [ ...n ] ) ]  
    | view_name  
    | rowset_function_limited   
    | table_valued_function  
 }  
 [  ]  
 [ FROM  [ ,...n ] ]  
 [ WHERE {   
       | { [ CURRENT OF  
          { { [ GLOBAL ] cursor_name }  
             | cursor_variable_name   
          }  
       ]  
    }  
 }  
]
 [ OPTION (  [ ,...n ] ) ]  
[; ]

::= {
 [ server_name.database_name.schema_name.  
    | database_name. [ schema_name ] .  
    | schema_name.  
 ]  
    table_or_view_name  
}

Câu lệnh TRUNCATE:

Cú pháp chung:

TRUNCATE TABLE

 [ { database_name .[ schema_name ] . | schema_name . } ]  
 table_name  
[ ; ]

Lệnh TRUNCATE này sẽ thực hiện việc phân bổ lại trang chứa dữ liệu trong bảng, và toàn bộ phần dữ liệu đó sẽ bị xóa bỏ hoàn toàn sau khi thực hiện. Nếu xét về khía cạnh tốc độ thì TRUNCATE nhanh hơn DELETE khá nhiều dựa vào lượng thông tin được lưu lại trong phần Transaction Log, và cũng vì vậy nếu những bản ghi nào đó vô tình bị xóa bỏ thì sẽ không thể khôi phục lại được. Còn về bản chất, TRUNCATE là toán tử Data Definition Language – DDL, cũng có nghĩa rằng chúng ta cần tối thiểu ALTER TABLE hoặc phân quyền cao hơn để thực hiện. Nhưng mức phân quyền TRUNCATE TABLE lại không tồn tại. Nếu 1 bảng dữ liệu sau khi thực hiện TRUNCATE có tính năng tăng số lượng cột thì sẽ tự động được sắp xếp lại theo phần định nghĩa ban đầu. Bên cạnh đó, còn có một số hạn chế với cú pháp TRUNCATE, và không thể được áp dụng trên những bảng cụ thể như tình huống dưới đây:

- Khi bảng dữ liệu được tham chiếu bởi Index View.

- Các khóa liên kết ngoài được rút ngắn.

- Bảng dữ liệu đó được dùng để sao chép.

- Bảng thuộc về cơ sở dữ liệu đang được log.

Khi nào cần sử dụng

Tùy vào từng tình huống cụ thể mà các bạn hãy sử dụng lệnh DELETE hoặc TRUNCATE sao cho phù hợp. Trong đó, cú pháp DELETE được sử dụng khá rộng rãi và phổ biến vì cho phép người dùng xác định rõ ràng bản ghi nào cần xóa, được kết hợp cùng với lệnh JOINS và một vài tham số khác. Khả năng ngăn chặn có thể xảy ra khi chúng ta dùng DELETE để xóa bỏ 1 lượng dữ liệu khá lớn, do vậy người dùng cần tuyệt đối cẩn thận khi thao tác. Còn lại, lệnh TRUNCATE sẽ giúp người quản trị xóa toàn bộ 1 bảng dữ liệu nào đó một cách đơn giản và nhanh chóng.