SQL Error: ORA-00907: missing right parenthesis st

2019-08-09 02:51发布

问题:

CREATE TABLE ADMIN (
    A_EMP_ID                   CHAR 5 BYTE                            NOT NULL,
    ADMIN_START_DATE           DATE                 DEFAULT SYSDATE       NOT NULL,
    ADMIN_END_DATE             DATE                                       NULL,
    DIVERSITY_TRAINING_CERT      CHAR(1 BYTE)       DEFAULT 'N'           NOT NULL,
    ADMIN_TITLE                  CHAR(40 BYTE)                            NULL,
    CONSTRAINT                   ADMIN_PK           PRIMARY KEY(A_EMP_ID),
  CONSTRAINT        ADMIN_FK1          
  FOREIGN KEY(A_EMP_ID)
  REFERENCES ADMIN(A_EMP_ID),
  CONSTRAINT                 ADMIN_DIVERSITY_CERT
      CHECK (DIVERSITY_TRAINING_CERT = 'N','Y'),
  CONSTRAINT                 ADMIN_END_DATE
      CHECK (<= 'ADMIN_START_DATE'),
    );



Error starting at line : 1 in command -
CREATE TABLE ADMIN (
    A_EMP_ID                   CHAR 5 BYTE                            NOT NULL,
    ADMIN_START_DATE           DATE                 DEFAULT SYSDATE       NOT NULL,
    ADMIN_END_DATE             DATE                                       NULL,
    DIVERSITY_TRAINING_CERT      CHAR(1 BYTE)       DEFAULT 'N'           NOT NULL,
    ADMIN_TITLE                  CHAR(40 BYTE)                            NULL,
    CONSTRAINT                   ADMIN_PK           PRIMARY KEY(A_EMP_ID),
  CONSTRAINT        ADMIN_FK1          
  FOREIGN KEY(A_EMP_ID)
  REFERENCES ADMIN(A_EMP_ID),
  CONSTRAINT                 ADMIN_DIVERSITY_CERT
      CHECK (DIVERSITY_TRAINING_CERT = 'N','Y'),
  CONSTRAINT                 ADMIN_END_DATE
      CHECK (<= 'ADMIN_START_DATE'),
    )
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:

The top part is my code and when I run it, I get the message from the bottom half. I'm thinking it's something to do with my foreign key, but I don't know what the exact solution is. Any help is greatly appreciated.

回答1:

There is a lot wrong with your statement.

  • A_EMP_ID CHAR 5 BYTE is missing the (..) around the length constraint
  • You most definitely do not want to use CHAR for the admin_title. UseVARCHAR2 instead.
  • DIVERSITY_TRAINING_CERT = 'N','Y' is not a valid expression. You probably want diversity_training_cert IN ('N','Y')
  • While FOREIGN KEY (a_emp_id) REFERENCES admin(a_emp_id) is syntactically correct, it doesn't makes sense. I guess you want a manager_id or something similar. And then something like FOREIGN KEY (manager_id) REFERENCES admin(a_emp_id).
    Alternatively you maybe intended to reference an employee table. In that case the a_emp_id data type must match the type of PK column that table.
  • CONSTRAINT ADMIN_END_DATE CHECK (<= 'ADMIN_START_DATE'), has three errors:

    • a column must not be enclosed in single quotes. So it has to be admin_start_date not 'admin_start_date'
    • a check constraint requires a proper condition. <= admin_start_date is not a condition, you need to compare the column it with something. Presumable admin_end_date
    • you have a comma , after that expression which is wrong as well.

Putting it all together you get:

CREATE TABLE admin 
(
    a_emp_id                  CHAR(5 BYTE) NOT NULL, 
    admin_start_date          DATE DEFAULT SYSDATE NOT NULL,
    admin_end_date            DATE NULL,
    diversity_training_cert   CHAR(1 BYTE)  DEFAULT 'N' NOT NULL,
    admin_title               VARCHAR2(40 BYTE) NULL,
    CONSTRAINT admin_pk 
        PRIMARY KEY(a_emp_id),
    CONSTRAINT admin_fk1 
        FOREIGN KEY (a_emp_id) REFERENCES admin(a_emp_id),
    CONSTRAINT admin_diversity_cert 
        CHECK (diversity_training_cert IN ('N','Y')), 
    CONSTRAINT admin_end_date 
        CHECK ( admin_end_date <= admin_start_date) 
);

Unrelated, but: there is also absolutely no need to write everything in upper case.



回答2:

You have a pending comma before the final parenthesis.



回答3:

CHAR 5 BYTE should be CHAR (5 BYTE) (but CHAR should not be used anyway try VARCHAR2 or NVARCHAR2...)

and the constraint <= 'ADMIN_START_DATE' is incorrect. This should have two values to compare