I'm currently learning SQL and I've installed oracle 11g express on my system. I'm trying to create a table however when I try to run the below command I get the following Error Message:
ERROR at line 3:
ORA-00904 : invalid identifier
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
Desc CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
Can anybody please tell me what I'm doing wrong?
Thanks for all the replies, I ran this command succesfully:
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
description CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
Really Appreciate the fast replies!
Chris
You have DESC in as a column name. While you can use it you will have to encompass it in quotes:
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
"Desc" CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
You will also have to use quotes every time you call it in a query. I recommend just changing that column to something else (maybe DESCRIPTION?)
Since DESC is a reserved word, you would have to enclose it in double quotes.
However, I would not recommend using reserved words for fields names, perhaps change to description or something similar
As already said several times, the error is caused here by the use of a reserved keyword unquoted as an identifier. For sake of completeness:
Oracle has an impressive list of reserved keywords.
Unquoted identifiers are internally converted upper-case by Oracle.
Quoted identifiers are case-sensitive
So:
CREATE TABLE T (DESC INT);
ORA-00904: : invalid identifier as DESC is a keyword
CREATE TABLE T (Desc INT);
ORA-00904: : invalid identifier same reason as unquoted identifiers are converted all upper-case
CREATE TABLE T ("DESC" INT);
Table created by using quotes, "DESC" is no longer recognized as a reserved keyword
INSERT INTO T("Desc") VALUES (1);
ORA-00904: "Desc": invalid identifier Quoted identifiers are case-sensitive. "DESC" is not the same columns as "Desc"
INSERT INTO T("DESC") VALUES (1);
1 row(s) inserted
That being said, you should avoid using a keyword as an identifier...
Related
CREATE type recommendation as object(
descriptions varchar2(200)
);
create type recomand as table of recommendation;
create type traitement_type as object (
id_traitement number(7),
duree varchar2(25),
recome recomand,
description varchar2(250)
);
CREATE TABLE Traitement (primary key(id_traitement))
nested table recome store as Les_recommendations;
i execute these commands and at the last query i got the error :
ORA-00904: : invalid identifier
any solutions ?
Your create table statement doesn't include a column list. It's interpreting "primary key" a column and spaces are not allowed. Look up the proper format for create table statements and PK constraints.
This question already has answers here:
getting error while creating table as ORA-00904: : invalid identifier in oracle database sql
(2 answers)
Closed 9 months ago.
I usually use SQL Server so declare variables in Oracle seem different a little.
Here is the table Im trying to create:
CREATE TABLE EMPLOYEE(
EmpNo char(10) CONSTRAINT PK_EmpNo PRIMARY KEY,
EmpName varchar2(30),
Birthday DATE not null,
DeptNo number,
MgrNo varchar2(30) not null,
StartDate DATE not null,
Salary number(7,2) not null,
Level number CONSTRAINT Level_ck check (Level > 0 AND Level < 8) not null,
Status number CONSTRAINT Status_ck check (Status >= 0 AND Status <= 2) not null,
Note varchar2(4000));
This is the error:
I think I declare Level and Status wrong because when I remove them out of the Table, the table can be created.Any suggestion?
LEVEL is a reserved word that cannot be used as an identifier. Every database has them; the complete list of reserved words for Oracle is here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Oracle-SQL-Reserved-Words.html#GUID-55C49D1E-BE08-4C50-A9DD-8593EB925612
STATUS is not a reserved word, and you should be able to create a column with that name.
Also note that table and column names in Oracle are not case-sensitive by default, so using camelCase isn't really appropriate (it will only be visible in your CREATE TABLE DDL command). A more standard naming convention is to use underscores to separate components of a label, such as START_DATE or DEPT_NR or TABLE_A.
I have a working PAINTER and PAINTING table which both work correctly. I am trying to Join them together where the PAINTING.PAINTER_Name (Artist who painted the painting) = PAINTER.AName (Artist Name). The only constraint is that the artist has to be born in the 19th century (Check WHERE Statement).
I have ran this code in I get an error reading "ERROR at line 3: ORA-00911: invalid character WHERE ABDate BETWEEN date’1800-01-01’ AND date’1899-12-29’".
I have attached the table schema and the query i am trying to tun. Any tips?
CREATE TABLE PAINTER
(AName varchar2 (15),
ABDate date,
ADdate date,
ACountry varchar2 (15),
constraint pkPAINTER PRIMARY KEY(AName));
CREATE TABLE PAINTING
(Painting_Name varchar2(15),
Year_Painted varchar2(15),
Est_Value varchar2(15),
Museum_Name varchar2(15),
PAINTER_Name varchar2(15),
constraint pkPainting_Name PRIMARY KEY (Painting_Name),
constraint fkPAINTING1 foreign key (PAINTER_Name) references PAINTER,
constraint fkPAINTING2 foreign key (Museum_Name) references MUSEUM);
SELECT PAINTING.Year_Painted, PAINTING.Painting_Name, PAINTER.ABDate
FROM PAINTING JOIN PAINTER ON PAINTING.PAINTER_Name = PAINTER.AName
WHERE ABDate BETWEEN '1800/01/01' AND '1899/12/31'
ORDER BY Painting_Name;
Try using a date literal:
SELECT p.Year_Painted, p.Painting_Name, pr.ABDate
FROM PAINTING p JOIN
PAINTER pr
ON p.PAINTER_Name = p.AName
WHERE pr.ABDate BETWEEN DATE '1800-01-01' AND DATE '1899-12-31'
ORDER BY Painting_Name;
EDIT:
Based on your error message:
ERROR at line 3: ORA-00911: invalid character WHERE ABDate BETWEEN date’1800-01-01’ AND date’1899-12-29’".
You are using "fancy" or "smart" single quotes for the dates. The proper delimiter for the date string is a single quote, and it should look like ', not ’.
You should use to_date or date literal to create date from string as follows:
WHERE ABDate BETWEEN DATE '1800-01-01' AND DATE '1899-12-31'
OR
WHERE ABDate BETWEEN TO_DATE('1800/01/01','YYYY/MM/DD')
AND TO_DATE('1899/12/31','YYYY/MM/DD')
-- Update
You have used wrong quotes (tilted quotes - ’). You need to use normal single quotes (')
date’1800-01-01’ -- opening and closing quotes are not normal single quotes
Use
date'1800-01-01'
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 creating a table and I typed this command:
SQL> create table accident(report_number integer primary key,
2 date varchar(20),
3 location varchar(20));
I got this error:
date varchar(20),
*
ERROR at line 2:
ORA-00904: : invalid identifier
Can anyone tell me where the error is and how to rectify it?
DATE is a reserved word and can't be used as a column name.
Date is a reserved word, to use it for column names, surround it with quotas "column-name"
ex:
create table abcd(
"date" date
);
insert into abcd values (sysdate);
select "date" from abcd;
But note that when using quoats, the column names will be case sensitive
ex:
select "Date" from abcd will result in an "Date": invalid identifier