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
Related
I am creating a table which is like this:
CREATE TABLE Peeps
(
Name VARCHAR(255) NOT NULL,
PhoneNum BIGINT NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
)
Every phone number has to start with 08. However when I tried insert there's an error because LIKE can't be used for numeric (or that's what my friend said). The alternative would be using VARCHAR for PhoneNum, but this is an assignment and we have to use numeric for the phone number.
If a phone number can start with a 0 then you need to use a string:
CREATE TABLE Peeps (
Name VARCHAR(255) NOT NULL,
PhoneNum VARCHAR(255) NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
);
Although you can use LIKE on a number, it is highly not recommended. What happens is that the number is converted to a string. However, that string will never start with a 0 -- well, at least never when the value is greater than 1.
I want to create this simple table in oracle application express but i keep getting the error
ORA-00904: : invalid identifier
and I have no idea why.
CREATE TABLE ArtWork (
ArtWorkId NUMBER (6,0) CONSTRAINT aw_pk PRIMARY KEY,
Name VARCHAR2 (20),
Desc VARCHAR2 (25)
);
desc is a reserved word (it is used to to specifiy the sort direction, for example in an order by clause).
You need to either surround it with double quotes, or better yet change your column name to something that does not clash with a language keyword, so you don't need to worry about it later on:
CREATE TABLE ArtWork (
ArtWorkId NUMBER (6,0) CONSTRAINT aw_pk PRIMARY KEY,
Name VARCHAR2 (20),
Description VARCHAR2 (25)
);
Reserved keywords shouldn't be used as identifiers as they are for implicit usages. Hence change desc to some other name like description
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)
);
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. []
I am new to writing SQL and using Oracle... so I'm sorry if this is obvious but I can't figure it out. It's telling me that I'm missing a right parenthesis but as far as I can tell they are all there. It seems to be a problem with the VARBINARY line but I don't know why.
CREATE TABLE DATA_VALUE
(
DATA_ID VARCHAR2(40) NOT NULL,
POSITION INT NOT NULL,
VALUE VARCHAR2(50),
BINARY_VALUE VARBINARY(50),
DATA_TYPE VARCHAR2(20),
CONSTRAINT DATA_VALUE_PK PRIMARY KEY(DATA_ID, POSITION)
);
VARBINARY is not an Oracle data type. A quick search suggests MySQL and SQL Server have it, at least, but not Oracle. Perhaps you need to explain what you want to store in that field. The closest I can think you might mean is RAW.
The valid built-in datatypes are listed in the documentation:
The RAW and LONG RAW data types store data that is not to be
explicitly converted by Oracle Database when moving data between
different systems. These data types are intended for binary data or
byte strings.
This Microsoft article suggests you should be using RAW as a replacement for VARBINARY too, at least for the size you're talking about.
CREATE TABLE DATA_VALUE
(
DATA_ID VARCHAR2(40) NOT NULL,
POSITION INT NOT NULL,
VALUE VARCHAR2(50),
BINARY_VALUE RAW(50),
DATA_TYPE VARCHAR2(20),
CONSTRAINT DATA_VALUE_PK PRIMARY KEY(DATA_ID, POSITION)
);
table DATA_VALUE created.