Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2744041
  • 博文数量: 423
  • 博客积分: 7770
  • 博客等级: 少将
  • 技术积分: 4766
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-09 11:58
个人简介

Oracle/DB2/Postgresql/Mysql/Hadoop/Greenplum/Postgres-xl/Mongodb

文章分类

全部博文(423)

文章存档

2019年(3)

2018年(6)

2017年(27)

2016年(23)

2015年(30)

2014年(16)

2013年(31)

2012年(73)

2011年(45)

2010年(14)

2009年(30)

2008年(30)

2007年(63)

2006年(32)

分类: Oracle

2008-07-18 09:49:00

                                          oracle对表的操作


create table student
(
    stuno int,
    stuname varchar(10) not null,
    stuBrith date default to_date('1980-1-1','YYYY-MM-DD')
    );
   
   
   
增加表字段:

alter table student add( stuPhone varchar(18));

删除字段:
alter table student drop column stuPhone

修改表字段属性
alter table student modify( stuname varchar(12) );


将表的某字段设为无用状态:

alter table student set unused column stXX;

将表的所以无用状态的字段进行删除:

alter table student drop unused columns;

 

通过修改表的方式增加约束:

alter table student add constraint pk_stuno primary key(stuno)


删除表约束

alter table student drop constraint pk_stuno

查询约束信息,
desc user_constraints;


 
 
  表分区知识
 
Oracle 允许用户对表进一步的规划,即对表进一步拆分,将表分成若干个逻辑部分,
每个部分称其为表分区 ,

分区的优点:

--增强可用性.单个分区出现故障,不影响其他分区,

--均衡的I/O,不同的分区可以映射到不同的磁盘,
---改善性能, 


表分区,--范围分区
第一种分文案:范围分区法
这种分区方法是根椐表中列值的范围对表进行分区,分区时,
首先依据列中的值可能的范围进行划分,
如某省对参加四六级考试的成绩进行整理并保存至数据库.
我们可以对分数段进行划分,

60<-------------P1

75<-------------P2

85<-------------P3

100<------------P4

   
   
SQL>      create table student
  2   (
  3      studentld integer not null,
  4      studententname varchar2(20),
  5      score   integer
  6   )
  7   Partition by range(score)
  8   (
  9      Partition p1 values    less than(60),
 10      Partition p2 values    less than(75),
 11      Partition p3 values   less than(85),
 12      Partition p4 values   less than(maxvalue)
 13    );

表已创建。

SQL> insert into student values(101,'Tom',50);

已创建 1 行。

SQL> insert into student values(&stun,'&Name',&score);
输入 stun 的值:  102
输入 name 的值:  Mike
输入 score 的值:  90
原值    1: insert into student values(&stun,'&Name',&score)
新值    1: insert into student values(102,'Mike',90)

已创建 1 行。

SQL> /
输入 stun 的值:  103
输入 name 的值:  Tyd
输入 score 的值:  89
原值    1: insert into student values(&stun,'&Name',&score)
新值    1: insert into student values(103,'Tyd',89)

已创建 1 行。

SQL> /
输入 stun 的值:  104
输入 name 的值:  Nil
输入 score 的值:  70
原值    1: insert into student values(&stun,'&Name',&score)
新值    1: insert into student values(104,'Nil',70)

已创建 1 行。

SQL> /
输入 stun 的值:  106
输入 name 的值:  Peter
输入 score 的值:  67
原值    1: insert into student values(&stun,'&Name',&score)
新值    1: insert into student values(106,'Peter',67)

已创建 1 行。

SQL> select * from  student;

 STUDENTLD STUDENTENTNAME            SCORE
---------- -------------------- ----------
       101 Tom                          50
       104 Nil                          70
       106 Peter                        67
       102 Mike                         90
       103 Tyd                          89

SQL> select * from student partition(p1);

 STUDENTLD STUDENTENTNAME            SCORE
---------- -------------------- ----------
       101 Tom                          50

SQL> select * from student partition (p2);

 STUDENTLD STUDENTENTNAME            SCORE
---------- -------------------- ----------
       104 Nil                          70
       106 Peter                        67

SQL> select * from student partition (p3);

未选定行

SQL> select * from student partition (p4);

 STUDENTLD STUDENTENTNAME            SCORE
---------- -------------------- ----------
       102 Mike                         90
       103 Tyd                          89

SQL>

 

 


   
 第二种方案:----散列分区
 散列分区法提供了一种通过指定分区编号来均匀地分布数据的方法,
 它通过Hash函数将数据映射到相应的分区上,
 它可使得数据均匀的分布到各分区上,各分区大小趋向一致,
 
 
 散列分区的语法
 
 Create table department
(
    Deptno int,
    Deptname varchar2(14)
)
 Partition by hash(deptno)
 (
 
 Partition p1,
 partition p2
 );
 
 
 表分区-----复合分区
 
 复合分区是先对数据进行范围分区,然后在每个子分区又进行散列分区的一种分区的方法,  
 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)
  );
 
 
 
  表分区-------列表分区
 
  列表分区允许用户明确地控制行到分区的映射,不同于范围分区,它允许按自然方式对无序
  和不相关的数据集进行分组和组织,
 
  例如:在客户表中记录着客户的国别信息,它们是中国,美国,法国,英国,加拿大,那么在创建表时,我们可以对表进行列表分区,
 
  create table customer
  ( custNo int,
    custname varchar(20),
    custState varchar(20)
    )
   Partition by list(custState)
   (
     Partition asia values('中国','韩国','新加坡'),
     Partition Europe values('英国','法国' ,'德国'),
     Partition  ameria('美国','加拿大','墨西哥'),
     )
    
    
 相表中播入数据:
 insert into student values(1001,'张三',52);
 
 查询表中数据
 Select * from student partition(P1)
 
 
 
 对表分区的维护:
 
 添加分区
 Alter table student add partition p5 values less than(120);
 
 删除分区:
 
 Alter table student drop partition p4;
 
 截短分区:
 
 Alter table student truncate partition p5;
 
 合并分区:
 Alter table student merge partitions p3,p4 into partition p6;
 
 
 涉及表分区的数据字典:
 User_tab_partitions
 User_ind_partitions
 
 
 
 
 
 
 
 
                      

阅读(1469) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~