SQL語句 清除資料表的所有記錄

2021-08-28 13:35:33 字數 1256 閱讀 8939

對資料庫進行清空,要求高效、快速。以下是比較有效的解決方法。

truncate table name

還有乙個排重的sql解決方法

alter ignore table dict add unique index(content); 建乙個索引。

truncate table

truncate是sql中的乙個刪除資料表內容的語句,用法是:

語法truncate table name

引數name

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

下面是對truncate語句在mssqlserver2000中用法和原理的說明:

truncate table 表名 速度快,而且效率高,因為:

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 不能用於參與了索引檢視的表。

對用truncate table刪除資料的表上增加資料時,要使用update statistics來維護索引資訊。

如果有rollback語句,delete操作將被撤銷,但truncate不會撤銷。

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

truncate table authors

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

補充引數:

truncate table name [drop/reuse storage]

drop storage:顯式指明釋放資料表和索引的空間

reuse storage:顯式指明不釋放資料表和索引的空間

sql刪除資料表及表記錄

truncate table 在功能上與不帶 where 子句的 delete 語句相同 二者均刪除表中的全部行。但 truncate table 比 delete 速度快,且使用的系統和事務日誌資源少。delete 語句每次刪除一行,並在事務日誌中為所刪除的每行記錄一項。truncate tabl...

sql語句刪除資料表重複欄位的方法

大家都可能遇到字段重複的情況,網上很多人在找方法,也給出了一些方法,但是有的方法是誤導大家,鐵牛寫出以下方法,方便大家使用 1.通過group by把重複的字段篩選出來,並建立臨時表tmp create table tmp as select max id as col1 from www grou...

sql語句刪除資料表重複欄位的方法

大家都可能遇到字段重複的情況,網上很多人在找方法,也給出了一些方法,但是有的方法是誤導大家,鐵牛寫出以下方法,方便大家使用 1.通過group by把重複的字段篩選出來,並建立臨時表tmp 1createtabletmpasselectmax id ascol1fromwwwgroupbydfdfd...