SQL Error: Missing Right Parenthesis in Line 1 ORA-00907 - sql

I've checked other similar questions. about repeating commas, error in commands but cant find any in my error. I've also searched examples of create tables to compare with mine, but fail to find any difference :(.
Below is the CREATE table statement:
CREATE TABLE DRIVER(L# VARCHAR(15) NOT NULL
, DNAME VARCHAR(75) NOT NULL
, STATUS VARCHAR(50) NOT NULL
, NRIC VARCHAR (15) NOT NULL
, PRIMARY KEY(L#)
, CANDIDATE KEY(NRIC)
);
Anyone can help me point out that i can't see or missing something,thanks (:

You can't specify CANDIDATE KEY like that in Oracle. The right way is to create a UNIQUE CONSTRAINT on your Candidate Key(s).
Like so.
Here's a working SQLFiddle: http://sqlfiddle.com/#!4/b392d/1
CREATE TABLE DRIVER(
L# VARCHAR(15) NOT NULL,
DNAME VARCHAR(75) NOT NULL,
STATUS VARCHAR(50) NOT NULL,
NRIC VARCHAR (15) NOT NULL,
PRIMARY KEY(L#),
CONSTRAINT UK_NRIC UNIQUE (NRIC)
);

ORA-00907 pretty much always indicates a syntax error. It happens when the parser finds a word which is not a key word in a position where it is expecting either a keyword or a right-hand bracket to close the current statement (hence Missing Right Parenthesis). Although sometime it can just be that we have an unmatched left-hand bracket: a decent text editor can help trace that.
In your case the intruding word is CANDIDATE. Syntax errors can be tricky for beginners. I recommend you familarise yourself with the Oracle documentation:it is online, complete and free, and has quite a lot of examples. Check it out . The list of reserved words could have helped you, because CANDIDATE is not on it, which is a big clue.

If you are going to use odd characters in a name, wrap it in square brackets. []

Related

ORA-00907: missing right parenthesis, and nothing is working

I've been sitting and looking at this code for a few hours and I don't understand where I went wrong, why it doesn't work.
ORA-00907: missing right parenthesis
I saw that this is a topic that is discussed alot but for some reason none of the examples I have seen has helped me. I'm pretty sure it's all right with the parentheses.
Here is the code:
CREATE TABLE ADT_FILIALA_AGENTIE (
id_f NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOT NULL ENABLE,
name varchar(30) NOT NULL,
telephone varchar(30) NOT NULL,
adress varchar(30) NOT NULL,
nr_an varchar(30) NOT NULL,
id_a int(11) NOT NULL,
PRIMARY KEY(id_f),
FOREIGN KEY(id_a) REFERENCES ADT_AGENTIE_PRINCIPALA(id_a)
);
You can't specify precision for the int data type in Oracle. You are using it for column id_a. int is shorthand for number(38,0).
To get the desired effect, replace that with number(11,0) - which means precision of 11 digits, of which zero decimal places.
Other than that, you would be well advised, in Oracle, to use varchar2(n) rather than varchar(n). For a very long time Oracle has warned us that in future releases varchar may be used for something else (even though that hasn't happened and is unlikely to happen).
To explain the error message: Right after the parser reads id_a int it expects (optional space and) a comma, or a constraint, etc. What it does not expect (since it makes no sense in that position) is an opening parenthesis, as you have in (11). The error handling is written to assume that at that point the create table statement should have ended - with a closing parenthesis. It doesn't find it, so it complains about that.
That's why you saw so many unrelated mentions of "missing right parenthesis" errors - they are often thrown by the parser when it finds something unexpected in a statement. The parser doesn't know what the real error is - it just sees something that doesn't make sense somewhere, and if at that point a closing parenthesis would have ended a valid statement, it throws that error.
You can't specify precision for the int data type in Oracle. You are using it for column id_a int.
CREATE TABLE ADT_FILIALA_AGENTIE ( id_f NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOT NULL ENABLE,
name varchar(30) NOT NULL,
telephone varchar(30) NOT NULL,
adress varchar(30) NOT NULL,
nr_an varchar(30) NOT NULL,
id_a int NOT NULL,
PRIMARY KEY(id_f),
FOREIGN KEY(id_a) REFERENCES ADT_AGENTIE_PRINCIPALA(id_a) );

Can't create table with script because of ENUM

I'm currently learning some basic stuff about sql in school. We received an exercise in which consists in creating a script that creates tables. We have a schema and we need to recreate it.
I'm having some issues with this one:
When I run the script, it shows this error:
CREATE TABLE "EMPLOYEE" ( "BIRTH_DATE" DATE , "FIRST_NAME" VARCHAR(14) , "LAST_NAME" VARCHAR(16) , "GENDER" ENUM('M','F') , "HIRE_DATE" DATE ) IN "TS_EMPLOYEE"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "(" was found following "16) , "GENDER" ENUM".
Expected tokens may include: "DEFAULT". SQLSTATE=42601
I looked for the error on the internet and thought I should specify the DEFAULT. For this reason I modified the script adding this part:
"GENDER" ENUM('M','F')DEFAULT 'M' ,
Unfortunately it didn't help me much, since it indicates me the same mistake as before.
Does anyone know where I am wrong? Or what I could change?
Any kind of help is appreciated! ^^
I don't think DB2 supports enums. If you are using DB2, then use a check constraint:
CREATE TABLE EMPLOYEE (
BIRTH_DATE DATE,
FIRST_NAME VARCHAR(14),
LAST_NAME VARCHAR(16),
GENDER CHAR(1),
HIRE_DATE DATE,
CHECK (GENDER IN ('M', 'F'))
);
Notes:
I removed the double quotes. Just don't use them for identifiers. They only clutter queries and introduce the possibilities for strange errors.
I think such a table should have a primary key, although I have not added one.
The lengths of the strings for the names seems unnecessarily short.

Elementary Oracle SQL Error message ORA-00900

I'm fairly new to writing SQL but I have a decent understanding of the basics at this point. I am trying to figure out why I am receiving the following error message: ORA-00900: invalid SQL statement.
Additionally, it would help if I could find which line is causing the error.
CREATE TABLE ADVENTURE_TRIP (
COLUMN CHAR(15) PRIMARY KEY,
TYPE CHAR(15),
LENGTH CHAR(15),
DECIMAL_PLACES CHAR(15),
NULLS_ALLOWED CHAR(15) NOT NULL,
DESCRIPTION CHAR(25)
);
The word COLUMN is a reserved word, and is not appropriate for a column name. You should choose a better name for the column.
Although TYPE is also a reserved word, it is allowed as a column name. I would change that too.
Also, usually VARCHAR2() is preferred over CHAR() for string types in Oracle.
The list of reserved words is here.
#Gordon described the correct justification.
Reserved keywords should be avoided.
But, If you really want to create the table as mentioned in your question then you can use the following code:
CREATE TABLE ADVENTURE_TRIP (
"COLUMN" CHAR(15) PRIMARY KEY,
"TYPE" CHAR(15),
LENGTH CHAR(15),
DECIMAL_PLACES CHAR(15),
NULLS_ALLOWED CHAR(15) NOT NULL,
DESCRIPTION CHAR(25)
);
Double quotes can be used for such purpose.
This is just to make you understand but in the real world, you must avoid using reserved keywords.
Here is the Demo

Oracle SQL - "missing keyword"

I am trying to create a simple table in Oracle SQL. The counterpart code works fine in SQL management studio, but not in Oracle APEX.
The following query:
CREATE TABLE Conference(
ConferenceID NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 100
INCREMENT BY 10
MINVALUE 100
MAXVALUE 100000
NO CYCLE),
Director VARCHAR(25) NOT NULL,
School Size NUMBER NOT NULL,
Location VARCHAR(50) NOT NULL,
CONSTRAINT pk_Conference PRIMARY KEY (ConferenceID)
);
is repeatedly met with the following error:
ORA-02000: missing ( keyword
I have done my due diligence searching for a solution to this problem here, here, and here.
I checked over the identity column section, as well as the Primary Key syntax here, and everything appears to look right. Despite this, I cannot find a solution.
Okay, in the docs, take a look at the identity options for a column definition. It's a small typo in this case - the NOCYCLE option is one word; there's no space.
You have one other problem, which is that School Size is not a valid column name. There's a space in it, and it's not quoted. You could do either School_Size or "School Size". I'd recommend the first one, since double-quoted column names are case-sensitive and really annoying to use.
Edit: Also, they're technically synonyms, but Oracle recommends using VARCHAR2 instead of VARCHAR.
CREATE TABLE Conference(
ConferenceID NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 100
INCREMENT BY 10
MINVALUE 100
MAXVALUE 100000
NOCYCLE),
Director VARCHAR2(25) NOT NULL,
School_Size NUMBER NOT NULL,
Location VARCHAR2(50) NOT NULL,
CONSTRAINT pk_Conference PRIMARY KEY (ConferenceID)
);

phpMyAdmin creating an attribute with '#" character in its name

I am trying to create the following table.
CREATE TABLE customer
(
cust# CHAR(3)NOT NULL ,
cname VARCHAR(30) NOT NULL ,
city VARCHAR(20) NOT NULL,
PRIMARY KEY (cust#)
)engine=InnoDB;
I am getting the following error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cname VARCHAR(30) NOT NULL , city VARCHAR(20) NOT NULL, PRIMARY KEY (cust#' at line 4
I have confirmed that the problem is with the '#' by replacing it with 'custNum'.
However, I must use the '#' sign. I know I can rename the field in the myPhpAdmin interface to cust# but I need to know how to escape it in the SQL statement.
*Edit to say I have already tried '\#'
Thanks
I was able to answer my own question and I'm posting here in case someone else has the same problem.
You have to use '`' around the name. So `cust#` did the trick.