为什么这个 CREATE 命令不是 运行?

Why is this CREATE command not running?

这是脚本的一部分,整个脚本运行没有错误。我按照脚本做了一些小改动,只是更改了名称等,并丢弃了一些命令,如 'create or replace force view'、sequences.

CREATE TABLE  "EMPLOYEES"
   ("EMPLOYEE_ID" NUMBER(6,0),
    "FIRST_NAME" VARCHAR2(20),
    "LAST_NAME" VARCHAR2(25) CONSTRAINT "EMP_LAST_NAME_NN" NOT NULL ENABLE,
    "EMAIL" VARCHAR2(25) CONSTRAINT "EMP_EMAIL_NN" NOT NULL ENABLE,
    "PHONE_NUMBER" VARCHAR2(20),
    "HIRE_DATE" DATE CONSTRAINT "EMP_HIRE_DATE_NN" NOT NULL ENABLE,
    "JOB_ID" VARCHAR2(10) CONSTRAINT "EMP_JOB_NN" NOT NULL ENABLE,
    "SALARY" NUMBER(8,2),
    "COMMISSION_PCT" NUMBER(2,2),
    "MANAGER_ID" NUMBER(6,0),
    "DEPARTMENT_ID" NUMBER(4,0),
        "BONUS" VARCHAR2(5),
     CONSTRAINT "EMP_SALARY_MIN" CHECK (salary > 0) ENABLE,
     CONSTRAINT "EMP_ID_PK" PRIMARY KEY ("EMPLOYEE_ID")
  USING INDEX  ENABLE,
     CONSTRAINT "EMP_EMAIL_UK" UNIQUE ("EMAIL")
  USING INDEX  ENABLE
  );

这是我从那里编辑的 'CREATE TABLE' 命令之一。

CREATE TABLE  "PRODUCT"
   ("Prod_ID" VARCHAR2(10) CONSTRAINT "Prod_ID_NN" NOT NULL ENABLE,
    "Prod_Price" NUMBER(3,2) CONSTRAINT "Prod_Price_NN" NOT NULL ENABLE,
    "Prod_Avail" VARCHAR2(20) CONSTRAINT "Prod_Price_NN" NOT NULL ENABLE,
    "Prod_MDate" DATE CONSTRAINT "Prod_MDate_NN" NOT NULL ENABLE,
    "Prod_EDate" DATE CONSTRAINT "Prod_EDate_NN" NOT NULL ENABLE,
    "Cust_ID" NUMBER(4,0),
    "Sto_ID" VARCHAR(8) CONSTRAINT "Sto_ID_NN" NOT NULL ENABLE,
    "Sup_ID" VARCHAR(10),
    CONSTRAINT "Prod_ID_PK" PRIMARY KEY ("Prod_ID")
    CONSTRAINT "Prod_Date_Interval" CHECK (Prod_EDate > Prod_MDate) ENABLE,
    USING INDEX  ENABLE
   );

出现错误; ORA-00907: 缺少右括号

ORA-06512:在“SYS.WWV_DBMS_SQL_APEX_210200”,第 673 行

ORA-06512:在“SYS.DBMS_SYS_SQL”,第 1658 行

ORA-06512:在“SYS.WWV_DBMS_SQL_APEX_210200”,第 659 行

ORA-06512:在“APEX_210200.WWV_FLOW_DYNAMIC_EXEC”,第 1829 行

"Prod_Avail" VARCHAR2(20) 约束 "Prod_Price_NN" NOT NULL ENABLE, “Prod_MDate”日期约束“Prod_MDate_NN”不为空, “Prod_EDate”日期约束“Prod_EDate_NN”不为空,

"Cust_ID" NUMBER(4,0),

"Sto_ID" VARCHAR(8) 约束 "Sto_ID_NN" NOT NULL ENABLE,

我建议您在使用 Oracle 时忘记双引号。他们没有什么好,只有问题。

当您修复错误时

  • 在约束中将大小写混合的列名括在双引号中
  • 使用唯一的约束名称
  • 重新排列using index enable

然后 有效:

SQL> CREATE TABLE "PRODUCT"
  2  (
  3     "Prod_ID"      VARCHAR2 (10) CONSTRAINT "Prod_ID_NN" NOT NULL ENABLE,
  4     "Prod_Price"   NUMBER (3, 2) CONSTRAINT "Prod_Price_NN" NOT NULL ENABLE,
  5     "Prod_Avail"   VARCHAR2 (20) CONSTRAINT "Prod_Avail_NN" NOT NULL ENABLE,
  6     "Prod_MDate"   DATE CONSTRAINT "Prod_MDate_NN" NOT NULL ENABLE,
  7     "Prod_EDate"   DATE CONSTRAINT "Prod_EDate_NN" NOT NULL ENABLE,
  8     "Cust_ID"      NUMBER (4, 0),
  9     "Sto_ID"       VARCHAR (8) CONSTRAINT "Sto_ID_NN" NOT NULL ENABLE,
 10     "Sup_ID"       VARCHAR (10),
 11     CONSTRAINT "Prod_ID_PK" PRIMARY KEY ("Prod_ID") USING INDEX ENABLE,
 12     CONSTRAINT "Prod_Date_Interval" CHECK ("Prod_EDate" > "Prod_MDate") ENABLE
 13  );

Table created.

SQL>

但是 - 再一次 - 我不会那样做。我该怎么做?就像这样:

SQL> CREATE TABLE product
  2  (
  3     prod_id      VARCHAR2 (10) CONSTRAINT prod_id_nn NOT NULL ENABLE,
  4     prod_price   NUMBER (3, 2) CONSTRAINT prod_price_nn NOT NULL ENABLE,
  5     prod_avail   VARCHAR2 (20) CONSTRAINT prod_avail_nn NOT NULL ENABLE,
  6     prod_mdate   DATE CONSTRAINT prod_mdate_nn NOT NULL ENABLE,
  7     prod_edate   DATE CONSTRAINT prod_edate_nn NOT NULL ENABLE,
  8     cust_id      NUMBER (4, 0),
  9     sto_id       VARCHAR (8) CONSTRAINT sto_id_nn NOT NULL ENABLE,
 10     sup_id       VARCHAR (10),
 11     CONSTRAINT prod_id_pk PRIMARY KEY (prod_id) USING INDEX ENABLE,
 12     CONSTRAINT prod_date_interval CHECK (prod_edate > prod_mdate) ENABLE
 13  );

Table created.

SQL>