閱讀975 返回首頁    go 阿裏雲 go 技術社區[雲棲]


5.Oracle中的數據表

1.數據表的數據類型:
    varchar2(size) : 可變字符數據;
    char:固定長度字符數據;
    number:數值型;
    date:日期時間型;
    long:可變長度的字符型數據(2G);
    clob:單字節的大型字符對象(4G);
    raw and long raw : 二進製類型;
    blob:二進製大型對象,最大(4G);
    bfile:二進製數據,在數據庫外部存儲文件(4G);

2.表的創建:
   create table 表名
   (
      字段1 類型1,
      字段2 類型2,
         ...       ...
      字段n 類型n
    )
  create table student
  (
     stuno int,
     stname varchar(10)not null,
     stBirth date default to_date('1987-6-23','YYYY-MM-DD')
  );

3.數據表的維護:
  添加字段:
  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 stuname;
  刪除無用字段:
  alter table student drop unused columns;
  增加約束:
  alter table student add constraint pk_stuno primarykey(stuno);
  刪除約束:
  alter table student drop constraint pk_stuno;
  查看表約束:

  desc user_constraints;

4.表分區:
   第一種方案:範圍分區法
                        這種分區法事根據表中列值的範圍對表進行分區;
                        分區時,首先依據列中的值可能的範圍進行劃分;
                        如某省對參加四六級考試的成績進行整理並保存至數據庫。我們可以對於分數段進行如下劃分:
                           60---p1;
                           75---p2;
                           85---p3;
                           100--p4;
    create table student
    (
       student_ID integer not null,
       student_name varchar2(20),
       score integer
     )
     partition by range(score)
     (
         partition p1 values less than(60),
         partition p2 values less than(75),
         partition p3 values less than(85),
         partition p4 values less than(maxvalue)   
     );
     查詢表中數據:select * from student partition(p1);
   第二種方案:散列分區法
                        提供了一種通過指定分區編號來均勻的分布數據的方法;
                        它通過hash函數將數據映射到相應的分區上;
                        它可使得數據均勻的分布到各個分區上,各個分區大小趨於一致;
      create table department
       (
          deptno int,
          deptname varchar(14)
        )
       partition by hash(depno)
       (
           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 values('美國','加拿大','墨西哥')
          );
5.查看表分區語句:
      desc user_tab_partitions;
   查看表上的分區:
      select table_name as 表名,partition_name as 分區名,partition_position from user_tab_partitions;

最後更新:2017-04-02 04:00:23

  上一篇:go 自定義
  下一篇:go 2.Orcal中的SQL語句