asoka.hang's oracle/java blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            4 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          2006年4月13日 #

          大家知道,oracle中沒有提供修改約束條件的命令,如果要用到修改約束條件時,常規做法就是先drop掉當前約束再重新創建一個新約束條件,從應用層次而言,這樣做是不妥的。

          常規做法如下:
          SQL > ? create ? table ?test_constraint
          ??
          2
          ??(
          ??
          3 ??id? number
          ,
          ??
          4 ??name? varchar ( 25
          ),
          ??
          5 ??age? number ? constraint ?age_ct? check (age? between ? 0 ? and ? 99
          )
          ??
          6
          ??);

          表已創建。

          SQL
          > ? alter ? table ?test_constraint? drop ? constraint
          ?age_ct;

          表已更改。

          SQL
          > ? alter ? table ?test_constraint? add ? constraint ?age_ct? check (age? between ? 18 ? and ? 99
          );

          表已更改。

          tom在一篇文章中這樣講: “我會用兩條命令:一條增加一個新的約束,另一條刪除舊的約束。”,仔細理解也就是說他會先將新的約束條件進行添加以達到新的要求需求,而事后再對舊的約束條件予以drop,這種做法通過sqlplus表現如下:
          SQL > ? drop ? table ?test_constraint;

          表已丟棄。

          SQL
          > ? create ? table
          ?test_constraint
          ??
          2
          ??(
          ??
          3 ??id? number
          ,
          ??
          4 ??name? varchar ( 25
          ),
          ??
          5 ??age? number ? constraint ?age_ct? check (age? between ? 0 ? and ? 99
          )
          ??
          6
          ??);

          表已創建。

          SQL
          > ? alter ? table ?test_constraint? add ? constraint ?agenew_ct? check (age? between ? 18 ? and ? 99
          );

          表已更改。

          SQL
          > ? alter ? table ?test_constraint? drop ? constraint
          ?age_ct;

          表已更改。

          從應用層面上而言,更改一個約束名稱并不會受到影響,通過上面方法既將新的約束條件增加進去了又沒有影響到應用(先drop后add的做法在這里顯得是一種錯誤的做法)。
          posted @ 2006-04-19 17:30 asoka的oracle/java博客 閱讀(267) | 評論 (0)編輯 收藏

          case:要求在一張銷售信息表中查詢每件產品的最近銷售日期(不用PL/SQL實現)。

          創建test_purchase表:
          SQL > ? create ? table ?test_purchase(
          ??
          2 ??product_name? varchar2 ( 25
          ),
          ??
          3 ??salesperson? varchar2 ( 3
          ),
          ??
          4
          ??purchase_date?date,
          ??
          5 ??quantity? number ( 4 , 2
          )
          ??
          6
          ??);

          表已創建。

          往test_purchase表中插入數據:
          SQL > ? insert ? into ?test_purchase? values ( ' small?widget ' , ' ca ' ,to_date( ' 2003-7-14 ' , ' YYYY-MM-DD ' ), 1 );

          已創建?
          1
          ?行。

          SQL
          > ? insert ? into ?test_purchase? values ( ' medium?wodget ' , ' bb ' ,to_date( ' 2003-7-14 ' , ' YYYY-MM-DD ' ), 75
          );

          已創建?
          1
          ?行。

          SQL
          > ? insert ? into ?test_purchase? values ( ' chrome?phoobar ' , ' ga ' ,to_date( ' 2003-7-14 ' , ' YYYY-MM-DD ' ), 2
          );

          已創建?
          1
          ?行。

          SQL
          > ? insert ? into ?test_purchase? values ( ' small?widget ' , ' ga ' ,to_date( ' 2003-7-15 ' , ' YYYY-MM-DD ' ), 8
          );

          已創建?
          1
          ?行。

          SQL
          > ? insert ? into ?test_purchase? values ( ' medium?wodget ' , ' lb ' ,to_date( ' 2003-7-15 ' , ' YYYY-MM-DD ' ), 20
          );

          已創建?
          1
          ?行。

          SQL
          > ? insert ? into ?test_purchase? values ( ' round?snaphoo ' , ' ca ' ,to_date( ' 2003-7-16 ' , ' YYYY-MM-DD ' ), 5
          );

          已創建?
          1 ?行。

          關鍵的select 子查詢語句來了,呵呵

          SQL > ? select ? * ? from ?test_purchase? where (product_name,purchase_date)
          ??
          2 ?? in

          ??
          3 ??( select ?product_name, max (purchase_date)? from ?test_purchase? group ? by ?product_name);

          PRODUCT_NAME??????????????SAL?PURCHASE_D???QUANTITY
          -- -----------------------?---?----------?----------

          chrome?phoobar????????????ga?? 2003 - 07 - 14 ?????????? 2
          medium?wodget?????????????lb??
          2003 - 07 - 15 ????????? 20
          round ?snaphoo?????????????ca?? 2003 - 07 - 16 ?????????? 5
          small?widget??????????????ga??
          2003 - 07 - 15 ?????????? 8

          其實仔細想一想也不是很復雜,不過這條語句乍一想來如何寫還真有點困難,所以用了個妙字,:p

          posted @ 2006-04-13 23:04 asoka的oracle/java博客 閱讀(295) | 評論 (0)編輯 收藏

          case:在scott.emp中有一個字段為deptno用來存儲部門編號,在關系型數據庫中一個字段只能夠存儲一列值(像ENAME列只能夠存儲員工姓名的值),要想查詢emp與dept兩表間的關聯值還得運用主從表間的關系,查詢效率不高,在emp表中deptno字段并不是很復雜(因為dept表中僅三個字段)的情況下,oracle允許在emp表中的deptno字段里存儲一個表,這雖然違反關系型數據庫的規則,但是提高了性能。

          我們將舉一個入庫明細的例子來說明可變數組在上面類似情況的應用。

          創建一個入庫的明細類型mingxitype:
          SQL > ? create ? or ? replace ?type?mingxitype? as ?object
          ??
          2
          ??(
          ??
          3 ?????goodsid? varchar ( 15
          ),
          ??
          4 ?????incount? int
          ,
          ??
          5 ?????providerid? varchar ( 10
          )
          ??
          6
          ??);
          ??
          7 ?? /

          創建一個基于mingxitype類型的可變數組:
          SQL > ? create ? or ? replace ?type?arrmingxitype? as ?varray( 100 )? of ?mingxitype;
          ??
          2 ?? /

          創建一個產品入庫主表:
          SQL > ? create ? table ?instock
          ??
          2
          ??(
          ??
          3 ?????orderid? int ? primary ? key
          ,
          ??
          4
          ?????indate?date,
          ??
          5
          ?????mingxi?arrmingxitype
          ??
          6 ??);

          向主表中插入數據:
          SQL > ? insert ? into ?instock? values ( 1001 ,to_date( ' 2006-1-1 ' , ' YYYY-MM-DD ' ),arrmingxitype
          ??
          2 ??(mingxitype( ' 101 ' , 10 , ' S01 ' ),mingxitype( ' 102 ' , 30 , ' S05 ' )));

          通過查詢可以查出主表instock中的字段mingxi中其實包括了一張表的內容,因為顯示出來的arrmingxitype可變數組內容比較零亂,降低了可讀性,運用PL/SQL可以讀出,但是這里我們用table()函數即可完成這個輸出修改,SQL語句如下:
          SQL > ? select ? * ? from ? table ( select ?s.mingxi? from ?instock?s? where ?orderid = 1001 );

          GOODSID????????????INCOUNT?PROVIDERID
          -- -------------?----------?----------

          101 ????????????????????? 10 ?S01
          102 ????????????????????? 30 ?S05

          這樣,通過在表中插入一個可變數組就將文中case里的問題給解決了。

          可變數組也有一定的缺陷,比如更新起來比較麻煩,因為不能更新varray中的單個元素,也就意味著,如果要去更新varray中的某個字段,需得將整個varray中的數據全部重新輸入一次,所以,可變數組在應用過程中需得慎重選擇,可以將其應用于一些平常不需要去修改數據的環境中,比如醫院里醫生開的處方歸檔資料庫等case中。
          posted @ 2006-04-13 16:18 asoka的oracle/java博客 閱讀(648) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 吕梁市| 饶阳县| 松江区| 姚安县| 新干县| 大埔区| 浙江省| 武汉市| 台山市| 阳春市| 龙里县| 永寿县| 日土县| 思南县| 大英县| 宽城| 兰坪| 巫溪县| 冷水江市| 邵阳县| 仁怀市| 安庆市| 博野县| 澄城县| 江西省| 梨树县| 隆德县| 驻马店市| 温宿县| 沛县| 瓮安县| 海宁市| 丹江口市| 库车县| 金坛市| 贵溪市| 柳江县| 蒲江县| 灌阳县| 平乐县| 鹤峰县|