SQL 00904. 00000 - "%s: invalid identifier" When creating a table - oracle

When I create a table like this:
CREATE TABLE "Movie_list" (
"Movie_id" NUMBER(8) NOT NULL,
"Company" VARCHAR2(30) NOT NULL,
"Rating" DECIMAL(5,1) NOT NULL,
"Storyline" VARCHAR2(255) NOT NULL,
"Award_id" VARCHAR2(255) NOT NULL,
"Cast_and_Crew_id" VARCHAR2(255) NOT NULL,
CONSTRAINT Movie_pk PRIMARY KEY (Movie_id),
);
An Error report is producded -
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
I don't see any problem, please help.

Errors:
Table and column names enclosed in quotes
NUMBER and VARCHAR2 are not a valid mysql type
A , is at the end of the constraint specification which is invalid

The main problem is in your primary key specification, which reads
CONSTRAINT Movie_pk PRIMARY KEY (Movie_id),
However, in the table definition you've quoted the name of this field, i.e.
"Movie_id" NUMBER(8) NOT NULL,
Because the name of this field (and in fact every field in the table, and the name of the table itself) is quoted the names are stored by the database as the specified mixed-case name, and because Oracle converts all unquoted identifiers to UPPER_CASE it means that EVERY TIME YOU REFER TO THIS TABLE AND ITS FIELDS, YOU MUST QUOTE THEM. So your constraint should be
CONSTRAINT Movie_pk PRIMARY KEY ("Movie_id")
My recommendation is that you dispense with the quoted "Mixed_case" names. Get rid of the quotes and Oracle will store the names in UPPER_CASE internally. You can still refer to them using Mixed_Case if you want to but you won't have to "Quote_Them_Every_Time_You_Use_Them", which gets really old and reads very badly. I suggest you change you table definition to
CREATE TABLE Movie_list (
Movie_id NUMBER(8) NOT NULL,
Company VARCHAR2(30) NOT NULL,
Rating DECIMAL(5,1) NOT NULL,
Storyline VARCHAR2(255) NOT NULL,
Award_id VARCHAR2(255) NOT NULL,
Cast_and_Crew_id VARCHAR2(255) NOT NULL,
CONSTRAINT Movie_pk PRIMARY KEY (Movie_id)
);
Best of luck.

This also happened to me when I was trying to use some sort of keyword as a column name.
I tried to create a table with a column name of "uid" (without "" evidently), and i was getting this error:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Changing the column's name solved the error.

Related

Oracle pushing me ORA-00902: invalid datatype

create table accountDetails(
accountNumber int unique,
customerId int unique,
balance int not null,
password varchar(255) not null,
type varchar(255) not null check(type in ('Savings','Current')),
primary key(accountNumber,customerId) )
create table statusDetails(
customerId int references accountDetails(customerId),
primarykey(customerId))
The last table resulted in an error
The last table resulted in an error
ORA-00902: invalid datatype error happens when we try to define a column using an invalid datatype. It's logical really.
Now, you think you haven't declared a column with an invalid datatype, because you think statusdetails table has only one column, customerid. But if you look at your actual statement you follow that column with this:
primarykey(customerId))
Because you mis-typed primary key Oracle treats that line as an attempt to create a second column. Hence the error, because (customerId) is an invalid datatype. So all you need do is pop in the missing space and Oracle will create the table for you.
create table statusDetails(
customerId int references accountDetails(customerId),
primary key(customerId))
You had a compilation error caused by a simple typo. A key skill for a developer is the ability to turn a cool eye on our own code. I urge you to work on acquiring that skill as soon as you can.
Your second table declaration is all wrong. Try this:
CREATE TABLE statusdetails (
customerid INT,
CONSTRAINT fk_cust FOREIGN KEY ( customerid )
REFERENCES accountdetails ( customerid )
)
Note: using "int" data type maps to a NUMBER(38), which may not be what you want. Use the proper oracle data type names.

SQL Error: ORA-00906: missing left parenthesis

CREATE TABLE Customer_TBL
(CustomerID INTEGER NOT NULL PRIMARY KEY,
CustomerName VARCHAR NOT NULL,
JobPosition VARCHAR,
CompanyName VARCHAR NOT NULL,
USState VARCHAR NOT NULL,
ContactNo BIGINTEGER NOT NULL);
Error starting at line : 1 in command -
Error report - SQL Error: ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Biginteger is not supported in Oracle, use number instead. And you need to use varchar2(number of char/bytes) or varchar(number of char/bytes).
Why the error missing left parenthesis?
Because Oracle was expecting ( after VARHCHAR but it was not there.
CREATE TABLE Customer_TBL (CustomerID INTEGER NOT NULL PRIMARY KEY,
CustomerName VARCHAR2(20) NOT NULL,
JobPosition VARCHAR2(20),
CompanyName VARCHAR2(20) NOT NULL,
USState VARCHAR2(20) NOT NULL,
ContactNo NUMBER NOT NULL);
You need to specify a maximum size for VARCHAR fields, e.g: field_name VARCHAR(40),

Oracle 11g _ ORA-00904 error message with Insert statement

I created a simple table:
CREATE TABLE "ADVUPGRD"."GL_CAMPUSEMAILS"
("Campus" VARCHAR2(2 CHAR), "SEND_TO" VARCHAR2(60 CHAR), "SEND_CC"
VARCHAR2(250 CHAR), "SEND_BCC" VARCHAR2(60 CHAR))
The table got created, I can do select * from gl_campusemails and it gets me a blank row since I have not populated this table yet.
When I'm populating the table I'm using this:
INSERT INTO GL_CAMPUSEMAILS (Campus, Send_To, Send_CC, Send_BCC)
VALUES('CP', 'as#gmail.com', 'test#yahoo.com', 'test2#gmail.com');
but I got this error message:
Error starting at line : 8 in command -
INSERT INTO GL_CAMPUSEMAILS (Campus, Send_To, Send_CC, Send_BCC)
VALUES('CP', 'as#gmail.com', 'test#yahoo.com', 'test2#gmail.com')
Error at Command Line : 8 Column : 56
Error report -
SQL Error: ORA-00904: "SEND_BCC": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I googled and found a lot of posting but they're mostly related to the use of reserved words in the select statement.
I don't think the columns I used in here belong to any reserved words.What did I do wrong in here
If you use double quotes while creating the table, the column names are created exactly as you typed, case sensitive; thus, you insert should be:
INSERT INTO GL_CAMPUSEMAILS(
"Campus",
"SEND_TO",
"SEND_CC",
"SEND_BCC"
)
VALUES (
'CP',
'as#gmail.com',
'test#yahoo.com',
'test2#gmail.com'
);
If you create the table with no quotes, this will work fine
CREATE TABLE GL_CAMPUSEMAILS
(
Campus VARCHAR2(2 CHAR),
SEND_TO VARCHAR2(60 CHAR),
SEND_CC VARCHAR2(250 CHAR),
SEND_BCC VARCHAR2(60 CHAR)
);
INSERT INTO GL_CAMPUSEMAILS(
Campus,
SEND_TO,
SEND_CC,
SEND_BCC
)
VALUES (
'CP',
'as#gmail.com',
'test#yahoo.com',
'test2#gmail.com'
);
Notice that not using double quotes, Oracle will consider all the objects with upper case names; so, for example, "CAMPUS", campus, CaMpUs will work, while "campus" will not

Missing parentheses in CREATE TABLE instruction

CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID CHAR(10) NOT NULL,
DESCRIPTION_PROD CHAR(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING INT(1),
PRIMARY KEY (PRODUCT_ID)
);
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Oracle is not MySQL so there is no INT(1) type:
CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID CHAR(10) NOT NULL,
DESCRIPTION_PROD CHAR(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING INT, -- here
PRIMARY KEY (PRODUCT_ID)
);
SqlFiddleDemo
If you don't need constant size of string, consider using VARCHAR2(30).
EDIT:
but i need those values to be between 1-5
So add check constraint:
CONSTRAINT chk_PROD_RATING CHECK (PROD_RATING BETWEEN 1 AND 5),
SqlFiddleDemo2
As already noted INT does not take a length constraint in Oracle - instead you could use NUMBER(1,0) which would restrict it to a single digit (-9 .. +9) and then you can further restrict it using a CHECK constraint.
CHAR(n) will also right pad the value with space (CHR(32)) characters so that it always contains the maximum number of characters. If you did not intend this then you should be using VARCHAR2(n) instead.
You also do not need a NOT NULL constraint on a column that is the PRIMARY KEY.
CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID VARCHAR2(10) CONSTRAINT MAJEST_PROD_2015__PROD_ID__PK PRIMARY KEY,
DESCRIPTION_PROD VARCHAR2(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING NUMBER(1,0) CONSTRAINT MAJEST_PROD_2015__PROD_RAT__CK CHECK ( PROD_RATING BETWEEN 1 AND 5 )
);
(also, should it be SEW_DATE or SOW_DATE? Since the next line talks about harvest then I would have thought "sow" was more apt.)

Oracle SQL Check Error

Why will my command not work when i use the check constraint? The table can be added when the check is not included.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ("Red", "Blue", "Green")),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
Error report -
SQL Error: ORA-02438: Column check constraint cannot reference other columns
02438. 00000 - "Column check constraint cannot reference other columns"
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constriant.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ('Red', 'Blue', 'Green')),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
No double quotes are allowed in oracle SQL

Resources