简体   繁体   中英

Unidentifiable error in Oracle SQL developer

I have been trying to create the table below for a long time:

CREATE TABLE Alumni (
 Alumni_ID           number GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
 First_Name          varchar2(20) NOT NULL,
 Last_Name           varchar2(20) NOT NULL,
 Gender              char(1) NOT NULL,
                CHECK (Gender IN('M','F')),
 Graduation_Year     number NOT NULL,
                check (Graduation_Year>=1980)AND(Graduation_Year>=2020),
 Course_taken        varchar2(255) not null,
 Award_Nominated     BOOLEAN,
 Award-Won           BOOLEAN,
 Phone_Number        VARCHAR2(15) not null,
 Email_Address       VARCHAR2(255) not null,
);

but I keep getting this error(beginning from line 1):

Error report -
ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:

After some research, I found that this error is caused by a syntax error. I have tried to my best to correct any syntax errors but it still does not function. Any help in finding the issue would be helpful. I am running Oracle database 19c with SQL Developer 20.4.1.

You say it's not identifiable, but the IDE (SQL Developer) is telling you there's a problem BEFORE you even execute your query/DDL.

在此处输入图像描述

Your CHECK constraint isn't valid.

Of course once you fix that, you'll run into the NEXT error. But the parser will continue to mark up your code as you progress. Or you can 'cheat' and look at one of the other answers here where someone has corrected your statement for you.

Your code has multiple errors:

  • A trailing comma.
  • The use of boolean in a table.
  • The lack of parentheses around check constraints.

The following works:

CREATE TABLE Alumni (
 Alumni_ID           number GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
 First_Name          varchar2(20) NOT NULL,
 Last_Name           varchar2(20) NOT NULL,
 Gender              char(1) NOT NULL,
                CHECK (Gender IN ('M', 'F')),
 Graduation_Year     number NOT NULL,
                check ((Graduation_Year >= 1980) AND (Graduation_Year >= 2020)),
 Course_taken        varchar2(255) not null,
 Award_Nominated     CHAR(1),
                CHECK (Award_Nominated IN ('T', 'F')),
 Award_Won           CHAR(1),
                CHECK (Award_Won IN ('T', 'F')),
 Phone_Number        VARCHAR2(15) not null,
 Email_Address       VARCHAR2(255) not null
);
check (Graduation_Year>=1980 AND Graduation_Year>=2020),

Seems like an odd constraint, if Graduation_Year >= 2020 it will always be >= 1980 too. So you can simplify and do the following instead:

check (Graduation_Year>=2020),

Seems the code had a lot more errors than I thought, ie: Use of boolean datatypes(bad habit from SQL Server), Using different parentheses for constraints, Hyphenated identifers. Here is the actual working query:

CREATE TABLE Alumni (
ID                  NUMBER GENERATED ALWAYS AS IDENTITY ,
First_Name          varchar2(20) NOT NULL,
Last_Name           varchar2(20) NOT NULL,
Gender              char(1) NOT NULL,
                CHECK (Gender IN('M','F')),
Graduation_Year     number NOT NULL,
                check (Graduation_Year>=1980 AND Graduation_Year<=2020),
Degree_Course       varchar2(255) not null,
Award_Nominated     CHAR(1),
                CHECK (Award_Nominated IN('Y','N')),
Award_Won           CHAR(1),
                CHECK (Award_Won IN('Y','N')),
Phone_Number        VARCHAR2(15) not null,
Email_Address       VARCHAR2(255) not null,
CONSTRAINT ALUM_PK PRIMARY KEY(ID)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM