看板 Database 關於我們 聯絡資訊
※ 引述《alexcwtzeng (狂瀉不止)》之銘言: : ※ 引述《ruby0104 (:))》之銘言: : : delete 跟 truncate 都可以rollback : : 提外話,這個問題面試的時候 還挺常問的 XD : Oracle SQL 的說明 : http://psoug.org/reference/truncate.html : SQL-Server 的說明也很多,說明了 TRUNCATE table 如何 rollback : ==== Example ===== : USE tempdb : CREATE TABLE t (c1 INT) : INSERT INTO t : SELECT BINARY_CHECKSUM(OrderID) FROM Northwind..Orders : SELECT * FROM t : BEGIN TRAN : TRUNCATE TABLE t : COMMIT TRAN : SELECT * FROM t : INSERT INTO t : SELECT BINARY_CHECKSUM(OrderID) FROM Northwind..Orders : BEGIN TRAN : TRUNCATE TABLE t : ROLLBACK TRAN : SELECT * FROM t : DROP TABLE t : ==== End Example ==== : 所以在 begin tran ... end tran 的 block 裏,所有的東西都可以 rollback。 : 但這是網路文章,我只是引述。SQL-Server 偶不會。 : 但是 Oracle Database 中,truncate table 與 delete table 是很大的不一樣滴。 : 在 ANSI/ISO standard 中說明 DDL 是屬於 autocommit 的指令。 : 而 truncate 是屬於 DDL 的指令。 Oracle Documents : Database Administrator's Guide - Using the TRUNCATE statement provides a fast, efficient method for deleting all rows from a table or cluster. A TRUNCATE statement does not generate any rollback information and it commits immediately. It is a DDL statement and cannot be rolled back. A TRUNCATE statement does not affect any structures associated with the table being truncated (constraints and triggers) or authorizations. A TRUNCATE statement also specifies whether space currently allocated for the table is returned to the containing tablespace after truncation --------------------- begin tran與 end tran 應該只有 sql server 可以用吧 ? 從 example的 northwind可以看出來是 sql server的 example sql server 不熟 XDDD 那應該是MS獨有的 feature ??? (←不確定) 不過 在什麼情況下 會需要用到 truncate ,然後再rollback? 我想應該有更好的方式 RDBMS幾家大廠在標準之外各做各的 哪天連 drop database 也能 rollback 其實也不用太意外 XDDDDDDDD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.145.230.2 ※ 編輯: MugenPower 來自: 202.145.230.2 (08/30 15:37)