我想根據Oracle表中列值的某些條件撰寫一個創建表腳本。
例如,我有一個名為 A 的表,在此表中,我們有 3 列 Country、Country Code 和 Country Short Name。
如果 Country 列有值,則 Country Code 和 Country Short Name 列可能有也可能沒有值。
如果 Country 列沒有值,則 Country Code 和 Country Short Name 列必須是強制性的。

如何在 Oracle DB 中為此撰寫創建表腳本?
uj5u.com熱心網友回復:
是的,可以做到,這是一個只有 2 列的示例,您可以輕松地將其擴展到 3 列或更多:
create table tcon (c1 number, c2 number, constraint ck check ((c1 is not null) or (c1 is null and c2 is not null)));
uj5u.com熱心網友回復:
您可以按照以下方式進行操作,只需確保條件正常即可
create table tableA (
country varchar2(20),
countryCode varchar2(20),
countryshort varchar2(20),
constraint chk check ((country is null and ( countryCode is not null or countryshort is not null) ) or (country is not null and (countryCode is null or countryshort is null)))
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/407696.html
標籤:
