【问题标题】:How to write a create table script based on certain column values in Oracle DB?如何根据 Oracle DB 中的某些列值编写创建表脚本?
【发布时间】:2022-01-05 13:02:49
【问题描述】:

我想根据Oracle表中列值的某些条件编写一个创建表脚本。

例如,我有一个名为 A 的表,在该表中,我们有 3 列 Country、Country Code 和 Country Short Name。

如果 Country 列有值,则 Country Code 和 Country Short Name 列可能有值,也可能没有值。

如果 Country 列没有值,则 Country Code 和 Country Short Name 列必须是强制性的。

如何在 Oracle DB 中为此编写创建表脚本?

【问题讨论】:

  • 是的,你可以!请查看create tableinsertNULL 的文档。
  • 创建一个表应该是一次性的。为什么要费心编写脚本,然后手动创建它并完成它,或者是否存在您试图解决的潜在问题,您没有提到

标签: sql database oracle oracle-sqldeveloper create-table


【解决方案1】:

可以,这是一个只有 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)));

【讨论】:

    【解决方案2】:

    你可以这样做,只要确保条件没问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2022-08-18
      • 2011-06-11
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多