表是數據庫中最基本的儲存數據邏輯單元,它由行和列組成,oracle將表分爲堆組織表(heap table),索引組織表(index organized table,IOT)和臨時表(temporary table) 本節講簡要介紹下oracle的表管理(heap table),主要包括添加,刪除列,更改列名,表名;創建相同的表,將表置爲只讀和讀寫模式,刪除表和回閃表等操作
1:添加列
yang SQL>select * from test order by 1;
ID BANNER
---------- --------------------------------
1 one
2 two
3 three
4 four
yang SQL>alter table test add (address varchar(10));
Table altered.
yang SQL>desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
BANNER VARCHAR2(32)
ADDRESS VARCHAR2(10)
2:刪除列
yang SQL>alter table test drop(address);
Table altered.
yang SQL>desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
BANNER VARCHAR2(32)
yang SQL>alter table test rename column id to num;
Table altered.
3:改列名
yang SQL>desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
NUM NUMBER(38)
BANNER VARCHAR2(32)
yang SQL>alter table test rename to t1;
Table altered.
4:更改表名
yang SQL>desc t1;
Name Null? Type
----------------------------------------- -------- ----------------------------
NUM NUMBER(38)
BANNER VARCHAR2(32)
5:創建相同的表,在創建大表的時候,可以增加併發和關閉日誌來提高效率
yang SQL>create table emp as select * from t1; //crate table emp nologing parallel(degree 4) as select * from t1;
Table created.
yang SQL>select * from emp order by 1;
NUM BANNER
---------- --------------------------------
1 one
2 two7
3 three
4 four
6:將表置爲只讀和讀寫模式
yang SQL>insert into emp values(5,'five');
1 row created.
yang SQL>commit;
Commit complete.
yang SQL>alter table emp read only;
Table altered.
yang SQL>insert into emp values(6,'six');
insert into emp values(6,'six')
*
ERROR at line 1:
ORA-12081: update operation not allowed on table "YANG"."EMP"
yang SQL>select count(*) from emp;
COUNT(*)
----------
5
yang SQL>alter table emp read write;
Table altered.
7:標記和刪除未用的列
yang SQL>alter table emp set unused (banner);
Table altered.
yang SQL>desc emp;
Name Null? Type
----------------------------------------- -------- ----------------------------
NUM NUMBER(38)
yang SQL>alter table emp drop unused columns;
Table altered.
8:刪除表與回閃表
yang SQL>drop table emp;
Table dropped.
yang SQL>desc emp;
ERROR:
ORA-04043: object emp does not exist
yang SQL>flashback table emp to before drop;
Flashback complete.
yang SQL>desc emp;
Name Null? Type
----------------------------------------- -------- ----------------------------
NUM NUMBER(38)
9:使用purge參數永久刪除表,若不使用purge參數,刪除的表將會保存在回收站中
yang SQL>drop table emp purge; //若要同時刪除表相關的約束條件,可以使用“cascade constraints”參數
Table dropped.
yang SQL>flashback table emp to before drop;
flashback table emp to before drop
*
ERROR at line 1:
ORA-38305: object not in RECYCLE BIN
10:啓用表壓縮:
yang SQL>create table emp_address (id int,name char(10),alias char(10),address varchar2(16)) compress for all operations;
Table created.
yang SQL>desc emp_address;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
NAME CHAR(10)
ALIAS CHAR(10)
ADDRESS VARCHAR2(16)
yang SQL>select table_name,compression,compress_for from dba_tables where table_name='EMP_ADDRESS';
TABLE_NAME COMPRESS COMPRESS_FOR
------------------------------ -------- ------------
EMP_ADDRESS ENABLED OLTP
本文出自 “斬月” 博客,請務必保留此出處http://ylw6006.blog.51cto.com/470441/461044