大家知道,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的做法在這里顯得是一種錯誤的做法)。
常規做法如下:
















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




















從應用層面上而言,更改一個約束名稱并不會受到影響,通過上面方法既將新的約束條件增加進去了又沒有影響到應用(先drop后add的做法在這里顯得是一種錯誤的做法)。