mysql drop table and purge

來源:互聯網
上載者:User

標籤:

一、drop表

執行drop table xx 語句

drop後的表被放在資源回收筒(user_recyclebin)裡,而不是直接刪除掉。這樣,資源回收筒裡的表資訊就可以被恢複,或徹底清除。

通過查詢資源回收筒user_recyclebin擷取被刪除的表資訊,然後使用語句

flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop [rename to <new_table_name>];

將資源回收筒裡的表恢複為原名稱或指定新名稱,表中資料不會丟失。

若要徹底刪除表,則使用語句:drop table <table_name> purge;

清除資源回收筒裡的資訊

清除指定表:purge table <table_name>;

清除目前使用者的資源回收筒:purge recyclebin;

清除所有使用者的資源回收筒:purge dba_recyclebin;

不清除,直接刪除則是:drop table xx purge;

舉例如下:

===============================================================================

SQL> select * from test1;

A B C

-- -- ----------

11 5

11 10

2 rows selected

SQL> create table test2 as select * from test1;

Table created

SQL> select * from test2;

A B C

-- -- ----------

11 5

11 10

2 rows selected

SQL> drop table test2;

Table dropped

SQL> select object_name, original_name, operation, type from user_recyclebin;

OBJECT_NAME ORIGINAL_NAME OPERATION TYPE

------------------------------ -------------------------------- --------- -------------------------

BIN$vQwemDg4R9mK9fYJNdYzvg==$0 TEST2 DROP TABLE

SQL> flashback table test2 to before drop rename to test3;--【to test3】將表重新命名

Done

SQL> select * from test3;

A B C

-- -- ----------

11 5

11 10

2 rows selected

SQL> select * from test2

ORA-00942: 表或視圖不存在

--徹底刪除表

SQL> drop table test3 purge;

Table dropped

二、清除表中的資料

truncate操作 同沒有where條件的delete操作十分相似,只是把表裡的資訊全部刪除,但是表依然存在。

例如:truncate table XX

Truncate不支援復原,並且不能truncate一個帶有外鍵的表,如果要刪除首先要取消外鍵,然後再刪除。

truncate table 後,有可能資料表空間仍沒有釋放,可以使用如下語句:

alter table 表名稱 deallocate UNUSED KEEP 0;

注意如果不加KEEP 0的話,資料表空間是不會釋放的。

例如:

alter table F_MINUTE_TD_NET_FHO_B7 deallocate UNUSED KEEP 0;

或者:

TRUNCATE TABLE (schema)table_name DROP(REUSE) STORAGE才能釋放資料表空間。

例如: truncate table test1 DROP STORAGE;

三、查詢分區表存在哪些分區:

查詢分區表的情況,可以在USER_TAB_PARTITIONS中查詢。例如:

select ‘alter table ‘||t.table_name ||‘ truncate partition ‘ || t.partition_name from USER_TAB_PARTITIONS t where t.table_name like ‘F_%‘

清除指定某個分區表的分區資料:

alter table 表名稱 truncate partition 分區名稱;

四、清除分區表佔用的空間:

alter table 表名稱 DROP partition 分區名稱;

例如:

alter table F_HOUR_TD_NET_MPVOICE DROP partition P_09121913 ;

五、查詢資料表空間資訊

可以利用如下語句查詢各表在儲存空間的使用分情況:

SELECT TABLESPACE_NAME,TO_CHAR(SUM(BYTES)/(1024*1024),‘999G999D999‘) CNT_MB FROM DBA_EXTENTS WHERE OWNER=‘&OWNER‘ AND SEGMENT_NAME=‘&TABLE_NAME‘ AND SEGMENT_TYPE LIKE ‘TABLE%‘ GROUP BY TABLESPACE_NAME;

可以使用如下語句,查詢儲存空間情況:

Select Tablespace_Name, Sum(bytes)/1024/1024 From Dba_Segments group By Tablespace_Name

六、查詢使用者下的表

如果你的使用者權限不是DBA:

那你用

select * from user_tables;

可以查詢到目前使用者所擁有的表。

如果是DBA使用者:

select * from dba_tables;

mysql drop table and purge

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.