oracle 表和表分区

来源:互联网 发布:微博域名搜索 编辑:程序博客网 时间:2024/06/05 01:00

oracle数据库中表有四种基本方式:

1.堆组织表:堆组织表就是普通的oracle表,数据的存储没有特定的顺序

2.索引组织表:存储B+树索引结构中排序的数据

3.集群表:集群表的列经常是一起被请求的,所以集群表是共享相同数据块的一组表的一部分

4.分区表:分区表允许将大量的数据根据不同的条件划分成称为分区的子表。分区在数据仓库环境中特别有用。


规则表:
1.表分区---范围分区

Create table Student(    StudentId integer not null,    StrdentName varchar2(20),    Score integer)Partition by range(Score)(    Partition p1 values less than(60),    Partition p1 values less than(75),    Partition p1 values less than(85),    Partition p1 values less than(maxvalue))select * from student partition(p1);

2.散列分区
通过Hash函数将数据均匀的映射到相应分区

Create table department(    Deptno int,    Deptname varchar2(14))Partition by hash(Deptno)(    Partition p1,    Partition p2)


3.复合分区
先进行范围分区,然后进行散列分区

Create table salgrade(    grade number,      losal number,    hisal number     )Partition by range(grade)Subpartition by hash(losal,hisal)(Partition p1 values less than(10),    (Subpartition sp1, Subpartition sp2),Partition p2 values less than(20),    (Subpartition sp3, Subpartition sp4),)

4.列表分区

Create table customer(    custno int,    custname varchar(20),    custstate varchar(20))Partition by list(custstate)(    Partition asia values('中国','韩国',''),    Partition europe values('英国'),    Partition ameria values('美国'),)
alter view viewname compile; 表结构可能更改,要重新编译视图

创建表的语法:

create table 表名(   字段名1  字段类型(长度) 是否为空,   字段名2  字段类型       是否为空);

-增加主键

alter table 表名 add constraint 主键名 primary key (字段名1); 

-增加外键:

alter table 表名   add constraint 外键名 foreign key (字段名1)      references 关联表 (字段名2);

在建立表格时就指定主键和外键

create table T_STU  (   STU_ID               char(5)                         not null,   STU_NAME             varchar2(8)                     not null,   constraint PK_T_STU primary key (STU_ID));

主键和外键一起建立:

create table T_SCORE  (   EXAM_SCORE           number(5,2),   EXAM_DATE            date,   AUTOID               number(10)                      not null,   STU_ID               char(5),   SUB_ID               char(3),   constraint PK_T_SCORE primary key (AUTOID),   constraint FK_T_SCORE_REFE foreign key (STU_ID)      references T_STU (STU_ID))