簡體   English   中英

如何根據 Oracle DB 中的某些列值編寫創建表腳本?

[英]How to write a create table script based on certain column values in Oracle DB?

我想根據 Oracle 表中列值的某些條件編寫一個創建表腳本。

例如,我有一個名為 A 的表,在此表中,我們有 3 列 Country、Country Code 和 Country Short Name。

如果 Country 列有值,則 Country Code 和 Country Short Name 列可能有也可能沒有值。

如果 Country 列沒有值,則 Country Code 和 Country Short Name 列必須是強制性的。

在此處輸入圖像描述

如何在 Oracle DB 中為此編寫創建表腳本?

是的,可以做到,這是一個只有 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)));

您可以按照以下方式進行操作,只需確保條件正常即可

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)))
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM