日期:2014-05-16  浏览次数:20425 次

4、模式对象管理 (table,index,seq)
这里说的模式对象:表,索引,约束,视图,同义词,序列。
模式是一个数据库对象的集合,为一个数据库用户所有,并且具有与该用户相同的名称
模式对象是由用户创建的逻辑结构,用来包含或引用他们的数据。
1.管理表
第一、创建表
create table user.table_name    --用户名.表名
(
{column1 datatype               --列名,类型
   [default expr]               --默认值,
   [column_constraint]},        --约束
{[column2 datatype ]...}
)
[constraint table_constraint]   --表间约束
[cluster cluster1 (column1,column2,...)] --聚合?
[pctfree n]                     --如果数据库剩余的自由空间少于pctfree自由空间,也就是说数据库已经写满,那么数据库就不会对其进行插入操作 
[pctused n]                     --每个update和delete操作后,数据库会比较该数据块的已用空间和pct_used,如果少于pct_used的已用空间,那么这个数据块被加入到自由列表中,供insert使用
[storage n]                     --inital 初始大小,next 下一次大小,minextents 最小分配数,maxextents 最大分配数,pctincrease 增长百分比
[tablespace tbs]                --表空间
[enable|disable]
[as query]                      --在查询的时候创建
列:
create table tab_01 nologging compress 
tablespace temp
as select * from tab_02 where 1=1 and ...;

查看表创建信息。
select owner,table_name,tablespace_name,initial_extent from dba_tables where owner ='scott';

第二、修改表结构
alter table tab_01 add(addName varchar2(30));--添加列
alter table tab_01 modify(addName varchar2(100));--修改列
alter table tab_01 rename column addName to delName;--修改列名
alter table tab_01 drop column delName;--删除列
rename tab_01 to tab_02;      --重命名表
alter table tab_02 to tab_01; --重命名表
drop table tab_01[cascade constraints]; --删除表,包括约束
-------------------------------------------------------
--修改表空间的存储位置
使用alter table ... move ...重定位非分区表的数据到新的段中。
alter table tab_01 move 
storage (initial 20K 
  next 40K 
  minextents 2
  maxextents 20)

第三、监控表的存储空间
--1.对表进行分析统计
analyze table tab_01 compute statistics;
--2.估计统计
analyze table tab_01 estimate statistics;
--3.分析指定行记录样本
analyze table tab_01 estimate statistics sample 2000 rows;
--4.分析百分比样本
analyze table tab_01 estimate statistics sample 80 percent;
--5.删除统计数据
analyze table tab_01 delete statistics;
--6.查询分析结果
select table_naem ,num_rows,blocks,empt_blocks from dba_tables where table_name ='tab_01';

第四、外部表
外部表的作用?
------------------------------------
2.索引
索引在物理和逻辑上依赖于表的数据,作为独立的结构,需要存储空间的支持。在插入,更新,删除表中的数据时,数据库将自动维护索引
维护建议:
在插入数据后创建索引。
为提高性能,为索引列排序
限制每个表索引的数量
为每个索引表定义表空间
考虑创建并行索引 --????
--默认索引 B-tree索引
--创建索引
create index tab_index on tab_01 (tab_No,tab_Name) tablespace index_tbs;
--重建索引
ALTER INDEX tab_index rebuild online;
-----------------------
oracle 提供了监控索引的方式能够确定是否被使用,这个要在实战中学习。

3.视图
create or replace view tab_view as 
select t01.tab_No,t01.tab_name,t02.tab_age
from tab_01 t01 ,tab_02 t02
where t01.tab_No = t02.tab_no;

4.序列
--创建
create sequence tab_seq 
increment by 1 start with 1
nomaxvalue nocycle cache 0;
--修改
alter sequence tab_seq
increment by 10 maxvalue 10000
cycle cache20;
--删除
drop sequence tab_seq;