在Oracle SQL中创建默认约束

时间:2010-08-09 19:10:20

标签: sql oracle oracle10g

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

1 个答案:

答案 0 :(得分:4)

使用:

ALTER TABLE EXTDL_REPORTSETS
     MODIFY last_ASOFSTARTDATETIME DATE DEFAULT SYSDATE

我在挖掘10g reference documentation - this is for 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" ;