SQL 错误消息:ORA-00907:缺少右括号

SQL error message: ORA-00907: missing right parenthesis

CREATE TABLE visitors 
(
    V_ID NUMBER(6) 
        CONSTRAINT pk_visitors_V_ID PRIMARY KEY, 
    V_FN VARCHAR2(15) NOT NULL, 
    V_LN VARCHAR2(15) NOT NULL,
    V_Contact NUMBER(10) NOT NULL,
    V_Address CHAR(10) NOT NULL,
    DoB DATE NOT NULL,
    Covid_Vaccine NUMBER(1) 
        CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3'))
    R_ID NUMBER(4) 
        CONSTRAINT fk_residents_R_IDREFERENCES residents(R_ID), 
    Date DATE NOT NULL
); 

我正在尝试在 Apex Oracle 上为 SQL 创建此 table,但我不断收到此错误:

ORA-00907: missing right parenthesis.

各种错误(缺少逗号、无效列名(date 为数据类型保留)、缺少 references 关键字)。修复后:

SQL> CREATE TABLE visitors (
  2      v_id          NUMBER(6)    CONSTRAINT pk_visitors_v_id PRIMARY KEY,
  3      v_fn          VARCHAR2(15) NOT NULL,
  4      v_ln          VARCHAR2(15) NOT NULL,
  5      v_contact     NUMBER(10)   NOT NULL,
  6      v_address     CHAR(10)     NOT NULL,
  7      dob           DATE         NOT NULL,
  8      covid_vaccine NUMBER(1)    CONSTRAINT ck_visitors_covid_vaccine CHECK
  9                                   ( covid_vaccine IN ( '1', '2', '3' ) ),
 10      r_id          NUMBER(4)    CONSTRAINT fk_residents_r_idreferences
 11                                   REFERENCES residents(r_id),
 12      c_date        DATE         NOT NULL );

Table created.

SQL>

该错误是因为 Covid_Vaccine 列声明后缺少逗号。但是在更正这个之后你会得到 invalid identifier 错误,因为 date 是保留字。所以我将列名更改为 ColDate。 如果没有在 R_ID 列声明中放置 reference 关键字,您将面临另一个错误 ORA-02253: constraint specification not allowed here。请尝试以下创建 table 语句(现在可以使用)。

 create table residents(R_ID NUMBER(4) PRIMARY KEY);

 CREATE TABLE visitors (
  V_ID NUMBER(6) CONSTRAINT pk_visitors_V_ID PRIMARY KEY, 
  V_FN VARCHAR2(15) NOT NULL, 
  V_LN VARCHAR2(15) NOT NULL,
  V_Contact NUMBER(10) NOT NULL,
  V_Address CHAR(10) NOT NULL,
  DoB DATE NOT NULL,
  Covid_Vaccine NUMBER(1) CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3')),
  R_ID NUMBER(4) CONSTRAINT fk_residents_R_IDREFERENCES references residents(R_ID), 
  ColDate DATE NOT NULL
 ); 

db<>fiddle here