asoka.hang's oracle/java blog

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

          2006年4月19日 #

          大家知道,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)編輯 收藏

          主站蜘蛛池模板: 惠安县| 开原市| 博兴县| 霍林郭勒市| 乐至县| 扶余县| 台中市| 乐昌市| 乐东| 宁化县| 张掖市| 杨浦区| 嘉禾县| 横山县| 福贡县| 会宁县| 大城县| 读书| 瓦房店市| 莱阳市| 固镇县| 蚌埠市| 清涧县| 临沂市| 靖江市| 且末县| 恩平市| 镇赉县| 文安县| 榆林市| 常山县| 兴国县| 庆阳市| 芮城县| 邻水| 长武县| 麦盖提县| 临城县| 沧州市| 临颍县| 内江市|