刪除表資料delete和truncate

2022-08-02 17:54:11 字數 730 閱讀 6755

truncate 命令用法

語法truncate table name

引數name

是要截斷的表的名稱或要刪除其全部行的表的名稱。

注釋truncate table 在功能上與不帶 where 子句的 delete 語句相同:二者均刪除表中的全部行。但 truncate table 比 delete 速度快,且使用的系統和事務日誌資源少。

delete 語句每次刪除一行,並在事務日誌中為所刪除的每行記錄一項。truncate table 通過釋放儲存表資料所用的資料頁來刪除資料,並且只在事務日誌中記錄頁的釋放。

truncate table 刪除表中的所有行,但表結構及其列、約束、索引等保持不變。新行標識所用的計數值重置為該列的種子。如果想保留標識計數值,請改用 delete。如果要刪除表定義及其資料,請使用 drop table 語句。

對於由 foreign key 約束引用的表,不能使用 truncate table,而應使用不帶 where 子句的 delete 語句。由於 truncate table 不記錄在日誌中,所以它不能啟用觸發器。

truncate table 不能用於參與了索引檢視的表。

示例下例刪除 authors 表中的所有資料。

truncate table authors

許可權truncate table 許可權預設授予表所有者、sysadmin 固定伺服器角色成員、db_owner 和 db_ddladmin 固定資料庫角色成員且不可轉讓。

資料刪除 delete

資料刪除 在刪除的時候需要詢問是否真的需要刪除?同時在之後的專案中,刪除往往不是真的刪除,而是做刪除標記 語法 delete from 表名 where 條件 delete from teacher 刪除指定的人員資訊 delete from teacher where id 26 使用delete...

delete連表刪除

delete 多表刪除的使用 1 從資料表t1中把那些id值在資料表t2裡有匹配的記錄全刪除掉 delete t1 from t1,t2 where t1.id t2.id 或delete from t1 using t1,t2 where t1.id t2.id 2 從資料表t1裡在資料表t2裡沒...

SQL中多表連線delete刪除表資料

delete刪除多表資料,怎樣才能同時刪除多個關聯表的資料呢?這裡做了深入的解釋 1 delete from t1 where 條件 2 delete t1 from t1 where 條件 3 delete t1 from t1,t2 where 條件 4 delete t1,t2 from t1...