oracle数据库对象-表的分类

oracle中的一个表最多可以有1000列。

oracle中有9种类型的表:

1、堆组织表(heap organized table)
就是“普通”的标准数据库表。数据以堆的方式管理。堆组织表中记录是无序的,不以某种特定顺序来放置。

2、索引组织表(index organized table)
索引组织表的数据都存储在与其关联的索引中,对表的添加、更新、删除,只会导致索引的更新。查询时必须根据索引去查询数据。

索引组织表必须设定主键。

create table indextable (
id varchar2(10),
name varchar2(20),
constraint pk_idx primary key (id)
)
organization index;

insert into indextable values ('1', '1');
insert into indextable values ('2', '2');

对索引组织表查询:

select * from indextable where id = '2';
select * from indextable where name = '2';

第一个查询的是主键,执行计划走的是INDEX UNIQUE SCAN。第二个查询的非索引字段,执行计划走的是INDEX FAST FULL SCAN。说明索引组织表的数据是跟在索引后面的,执行计划会自动根据索引来查询。

3、索引聚簇表(index clustered table)
聚簇(cluster),可以把多个表的数据存储在同一个块上。类似于数据共用。

1)创建聚簇

create cluster emp_dep_cluster (depno number(2)) size 2048;

size选项告诉oracle与每个聚簇键值关联大约2048字节的数据,oracle会使用这个参数来计算每个块最多能放下多少个聚簇键。

2)创建聚簇键索引

create index emp_dep_cluster_idx on cluster emp_dep_cluster;

3)创建表格

create table department (
depno number(2) primary key,
depname varchar2(20)
)
cluster emp_dep_cluster(depno);

create table employee (
empno number primary key,
empname varchar2(20),
depno number(2) references department(depno)
)
cluster emp_dep_cluster(depno);

需要使用cluster关键字来指定基表的哪个列会映射到聚簇本身的聚簇键。这两个表的deptno列在磁盘上的位置是一样的。

4、散列聚簇表(hash clustered table)
这些表类似于聚簇表,但是不使用B*树索引聚簇键来定位数据,聚簇键索引被一个散列函数所取代。

1)创建散列聚簇

create cluster emp_dep_cluster (depno number(2)) hashkeys 100 size 2048 hash is depno;

2)创建表格

create table department (
depno number(2) primary key,
depname varchar2(20)
)
cluster emp_dep_cluster(depno);

create table employee (
empno number primary key,
empname varchar2(20),
depno number(2) references department(depno)
)
cluster emp_dep_cluster(depno);

5、有序散列聚簇表(sorted hash clustered table)
如果经常使用:
Select *
From t
Where KEY=:x
Order by SORTED_COLUMN
也就是说,要按某个键获取数据,但要求这些数据按另外1列/几列排序。通过使用有序散列聚簇,oracle可以返回数据而不用执行排序。这是通过插入时按键有序物理存储数据做到的。

我觉得像这类表是用在很专门的地方,平时遇到的机会比较小。所以就不细说了。

6、嵌套表
跳过,等用到了再说。

7、临时表
临时表(Temporary table)用于保存事务或会话期间的中间结果集。临时表中保存的数据只对当前会话可见,所有会话都看不到其他会话的数据;即使当前会话已经提交(commit)了数据,别的会话也看不到它的数据。

对于临时表,不存在多用户并发问题,因为一个会话不会因为使用一个临时表而阻塞另一个会话。即使我们“锁住”了临时表,也不会妨碍其他会话使用它们自己的临时表。

临时表分为基于会话的临时表和基于事务的临时表。

0)默认的临时表

create global temporary table temp
as
select * from emp where 1=0
/

默认的临时表是基于事务级的(on commit delete rows)。

1)基于会话的临时表

create global temporary table temp
on commit preserve rows
as
select * from emp where 1=0
/

on commit preserve rows显示的说明这是一个基于会话的临时表。临时表里的数据在退出会话或使用delete、truncate语句删除外一直存在。

只有在当前会话中才能查询临时表的记录。

2)基于事务的临时表

create global temporary table temp
on commit delete rows
as
select * from emp where 1=0
/

on commit delete rows显示的说明这是一个基于事务的临时表。

如果在会话中执行commit,临时表里的记录就会被清空。

临时表是每个会话都有的,在会话结束之后记录会被清空,但是表结构还是保存在数据库中的,并没有删除临时表。

8、对象表
对象表类似于C里面的结构体变量。先创建一个对象(object),再用这个对象创建表。

create or replace type address_type
as object
(city varchar2(30),
street varchar2(30),
state varchar2(2),
zip number
)
/

create or replace type person_type
as object
(name varchar2(30),
dob date,
home_address address_type,
work_address address_type
)
/

create table people of person_type
/

insert into people values ( 'Tom', '15-mar-1965',
address_type( 'Reston', '123 Main Street', 'Va', '45678' ),
address_type( 'Redwood', '1 Oracle Way', 'Ca', '23456' ) );

select * from people;

select name, p.home_address.city from people p;

9、小结
感觉平时用的多的表是,堆组织表、索引组织表、临时表这三种。

《oracle数据库对象-表的分类》上的2个想法

评论已关闭。