Articles Comments

Oracle DBA & All IT » Function, Oracle Tips, Tuning Performance » ความแตกต่างระหว่างคำสั่ง Truncate, Delete และ Drop

ความแตกต่างระหว่างคำสั่ง Truncate, Delete และ Drop

DELETE

  คำสั่ง DELETE จะใช้สำหรับลบข้อมูลออกจากตาราง สามารถใส่เงื่อนไขในการลบได้ด้วย  ถ้าำไม่มีการกำหนด เงื่อนไข จะเป็นการลบข้อมูลทั้งตาราง หลังจากที่มีการรันคำสั่ง DELETE จะต้องตามด้วยคำสั่ง COMMIT เป็นการยืนยันการลบข้อมูล หรือ ROLLBACK เพื่อยกเลิกการลบก่อนหน้า
 

SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)

———-

        14

SQL> DELETE FROM emp WHERE job = 'CLERK';

4 rows deleted.

SQL> COMMIT;

Commit complete.

SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)

———-

        10

TRUNCATE

TRUNCATE  จะทำการลบข้อมูลทั้งตาราง เมื่อรันคำสั่งนี้จะไม่สามารถ ROLLBACK ได้ ซึ่ง TRUNCATE จะเร็วกว่าการใช้คำสั่ง DELETE เพราะไม่จำเป็นต้องมีการไปเก็บข้อมูลไว้ใน undo เพื่อไว้สำหรับการ ROLLBACK

SQL> TRUNCATE TABLE emp;

Table truncated.

SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)

———-

         0

DROP

DROP จะเป็นการลบตารางออกจากฐานข้อมูล  รวมทั้ง index , privileges  ซึ่งหลังจากรันคำสั่งนี้แล้วจะไม่สามารถ ROLLBACK กลับมาได้

SQL> DROP TABLE emp;

Table dropped.

SQL> SELECT * FROM emp;

SELECT * FROM emp

             *

ERROR at line 1:

ORA-00942: table or view does not exist

DROP และ TRUNCATE เป็น คำสั่ง DDL แต่  DELETE เป็นคำสั่ง DML ซึ่ง DELETE สามารถที่จะทำการ ROLLBACK ข้อมูลได้ ขณะที่ DROP และ TRUNCATE ไม่สามารถ ROLLBACK ได้

ตั้งแต่ Oracle 10g  ขึ้นไป Table ที่มีการ Drop ไปแล้วสามารถเรียกกลับมาได้  โดยใช้คำสั่ง FLASHBACK
ตัวอย่าง:

SQL> FLASHBACK TABLE emp TO BEFORE DROP;

Flashback complete.

 

.. Preview : 51990

Related Search:

  • truncate table คือ
  • truncate
  • truncate table
  • trunc oracle คือ
  • truncate คือ
  • oracle trunc คือ
  • truncate table sql
  • sql truncate
  • truncate sql
  • sql truncate คือ

Written by

บอกเล่าสิ่งที่พบเจอมาในการทำงาน ประสบการณ์การทำงานด้าน DBA ถ่ายทอดกันด้วยภาษาง่ายๆ บ้านๆ " ทุกอย่างไม่อยาก แต่... แค่ตั้งใจไม่พอ ต้องลงมือทำ และทำ GoodLuck " Fanpage: www.facebook.com/DBAor .. "Oracle Database Consultant " ..

Filed under: Function, Oracle Tips, Tuning Performance · Tags: , , , , , , , ,

  • Cool knowledge! I have been hunting for everything similar to this for a time these days. Thanks!

  • Where exactly is the facebook like button ?

  • Thanks for spending some time to explain the terminlogy to the beginners!

  • Fantastic posting, I truly look forward to posts by you.

  • did anyone here know that 5 from 6 fellas love russian roulette?

  • Good article in addition to simple to help understand explanation. How do When i try obtaining permission to help publish part on the article around my approaching e-newsletter? Giving correct credit ratings to your account this author in addition to link towards webpage would not become a dilemma.

  • I agree but of course this isnt the case… another thing: what would people do if I won 3 billion dollars?

  • I agree but of course this isnt true… another thing: what would people do if you won 2 billion usd?