表是数据库中最基本的储存数据逻辑单元,它由行和列组成,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