asoka.hang's oracle/java blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            4 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
          大家知道,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 on 2006-04-19 17:30 asoka的oracle/java博客 閱讀(267) 評論(0)  編輯  收藏 所屬分類: Oracle
          主站蜘蛛池模板: 三河市| 兴城市| 邹城市| 瑞丽市| 扶绥县| 霸州市| 黄梅县| 台江县| 延安市| 穆棱市| 上犹县| 延长县| 曲松县| 青海省| 孟村| 鄯善县| 民勤县| 昌宁县| 新田县| 五指山市| 安化县| 五大连池市| 方正县| 佛坪县| 原阳县| 仙桃市| 迁安市| 远安县| 永昌县| 鹰潭市| 昆明市| 银川市| 上饶市| 天全县| 温宿县| 玛沁县| 特克斯县| 伊金霍洛旗| 井冈山市| 田东县| 彭州市|