在 Oracle SQL 中创建默认约束

发布于 2024-09-13 04:56:31 字数 1379 浏览 3 评论 0原文

我来自 SQL Server,正在学习一些 Oracle 语法。

这是我的表 DDL,

CREATE TABLE "CDR"."EXTDL_REPORTSETS"
  (
    "ID"                NUMBER(38,0) NOT NULL ENABLE,
    "SHORTNAME"         NUMBER(38,0) NOT NULL ENABLE,
    "DESCRIPTION"       NUMBER(38,0) NOT NULL ENABLE,
    "ASOFSTARTDATETIME" NUMBER(38,0) NOT NULL ENABLE,
    "ASOFENDDATETIME"   NUMBER(38,0) NOT NULL ENABLE,
    CONSTRAINT "PK_EXTDL_REPORTSETS" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CDR_DATA" ENABLE
  )
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
  )
  TABLESPACE "CDR_DATA" ;

我想将 ASOFSTARTDATETIME 默认为 SysDate。如何在 PL/SQL 中设置默认约束?

编辑

数据类型变得混乱,所以我更正了它。

CREATE TABLE CDR.ExtDL_Reportsets(
    Id                   NUMBER(38, 0)    NOT NULL,
    ShortName            VARCHAR2(255)    NOT NULL,
    Description          VARCHAR2(500)    NOT NULL,
    AsOfStartDateTime    DATE             NOT NULL,
    AsOfEndDateTime      DATE,
    CONSTRAINT PK_ExtDL_Reportsets PRIMARY KEY (Id)
)
;

Coming from SQL Server, I am learning some Oracle syntax.

This is my table DDL

CREATE TABLE "CDR"."EXTDL_REPORTSETS"
  (
    "ID"                NUMBER(38,0) NOT NULL ENABLE,
    "SHORTNAME"         NUMBER(38,0) NOT NULL ENABLE,
    "DESCRIPTION"       NUMBER(38,0) NOT NULL ENABLE,
    "ASOFSTARTDATETIME" NUMBER(38,0) NOT NULL ENABLE,
    "ASOFENDDATETIME"   NUMBER(38,0) NOT NULL ENABLE,
    CONSTRAINT "PK_EXTDL_REPORTSETS" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CDR_DATA" ENABLE
  )
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
  )
  TABLESPACE "CDR_DATA" ;

I want to default ASOFSTARTDATETIME to SysDate. How do I set a default constraint in PL/SQL?

EDIT

The datatypes had gotten messed up, so I corrected it.

CREATE TABLE CDR.ExtDL_Reportsets(
    Id                   NUMBER(38, 0)    NOT NULL,
    ShortName            VARCHAR2(255)    NOT NULL,
    Description          VARCHAR2(500)    NOT NULL,
    AsOfStartDateTime    DATE             NOT NULL,
    AsOfEndDateTime      DATE,
    CONSTRAINT PK_ExtDL_Reportsets PRIMARY KEY (Id)
)
;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-09-20 04:56:31

使用:

ALTER TABLE EXTDL_REPORTSETS
     MODIFY last_ASOFSTARTDATETIME DATE DEFAULT SYSDATE

我在挖掘 10g 参考时遇到问题文档 - 这是针对 11g 的。

如果您想在 CREATE TABLE 语句中执行此操作:

CREATE TABLE "CDR"."EXTDL_REPORTSETS" (
  "ID"                NUMBER(38,0) NOT NULL ENABLE,
  "SHORTNAME"         VARCHAR2(255) NOT NULL ENABLE,
  "DESCRIPTION"       VARCHAR2(500) NOT NULL ENABLE,
  "ASOFSTARTDATETIME" DATE DEFAULT SYSDATE NOT NULL ENABLE,
  "ASOFENDDATETIME"   DATE NOT NULL ENABLE,
  CONSTRAINT "PK_EXTDL_REPORTSETS" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CDR_DATA" ENABLE
)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE (
   INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
)
TABLESPACE "CDR_DATA" ;

Use:

ALTER TABLE EXTDL_REPORTSETS
     MODIFY last_ASOFSTARTDATETIME DATE DEFAULT SYSDATE

I'm having trouble digging up the 10g reference documentation - this is for 11g.

If you want to do it in the CREATE TABLE statement:

CREATE TABLE "CDR"."EXTDL_REPORTSETS" (
  "ID"                NUMBER(38,0) NOT NULL ENABLE,
  "SHORTNAME"         VARCHAR2(255) NOT NULL ENABLE,
  "DESCRIPTION"       VARCHAR2(500) NOT NULL ENABLE,
  "ASOFSTARTDATETIME" DATE DEFAULT SYSDATE NOT NULL ENABLE,
  "ASOFENDDATETIME"   DATE NOT NULL ENABLE,
  CONSTRAINT "PK_EXTDL_REPORTSETS" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CDR_DATA" ENABLE
)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE (
   INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
)
TABLESPACE "CDR_DATA" ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文