ERROR at line 3: ORA-00911: invalid character" when joining tables - sql

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'

Related

Invalid identifier when trying to create a table with SQL

I am trying to create a table and I get an invalid identifier error. What does this mean? I've looked over the code over and over. All of my other statements run perfectly. Can't figure out what the issue is with this.
Here is the statement that returns the error:
/* Create Transaction Table */
31 CREATE TABLE TRANSACTION(TxNbr INTEGER PRIMARY KEY,
32 TxCode CHAR(1) NOT NULL,
33 AccountNbr INTEGER NOT NULL,
34 Amount DECIMAL(13,2) NOT NULL,
35 Date DATE,
36 Time TIME,
37 RefNbr VARCHAR(3),
38 FOREIGN KEY(AccountNbr) REFERENCES ACCOUNT (AccountNbr) ON DELETE SET NULL,
39 FOREIGN KEY(TxCode) REFERENCES TX_TYPE (TxCode) ON DELETE SET NULL
40 );
Here is the error:
Error starting at line : 31 in command -
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
There are two problems with your code (not considering the foreign key constraints, that we cannot validate without actually knowing the structure of the related tables)
DATE is a reserved word in Oracle (that's a datatype), so it can't be used for a column name, unless you surround it with double quotes - but if you do so, then you need to quote the column everytime you access it. I would suggest just using a column name that does not conflict with a language keyword.
There is no TIME datatype in Oracle; the DATE datatype actually stores the date and time - which is generally better than separating the date and time portion in two different columns anyway anyway, since it makes date arithmetics easier. I would recommend just removing that column.
Consider:
CREATE TABLE TRANSACTION(
TxNbr INTEGER PRIMARY KEY,
TxCode CHAR(1) NOT NULL,
AccountNbr INTEGER NOT NULL,
Amount DECIMAL(13,2) NOT NULL,
TxDate DATE,
RefNbr VARCHAR(3)
FOREIGN KEY(AccountNbr) REFERENCES ACCOUNT (AccountNbr) ON DELETE SET NULL,
FOREIGN KEY(TxCode) REFERENCES TX_TYPE (TxCode) ON DELETE SET NULL
);

Getting ORA-00904: : invalid identifier in oracle application express

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

Missing Right Parenthesis in SQL

I'm new to learning SQL. When I create this table, it has an Asterix (*) under the first parenthesis of the "(dbClassID)" and says "missing right parenthesis"
Does anyone know why it does that and how I can fix it?
CREATE TABLE vod_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100)
CONSTRAINT vod_classification_PK PRIMARY KEY (dbClassId)
);
CONSTRAINT is part of table creation and need to be comma delimited as other column:
CREATE TABLE zz_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100),
CONSTRAINT vod_classification_PK PRIMARY KEY( dbClassId)
);
Tables contain columns and constraints
you are missing , here try this VARCHAR2(100),
For a single-column constraint, it's neater to define it inline as part of the column:
create table vod_classification
( dbclassid varchar2(4) not null constraint vod_classification_pk primary key
, dbdescription varchar2(100) not null constraint vod_classification_uk unique
);
I have corrected the CHAR column to the standard string type which is VARCHAR2 in Oracle.
(PK columns will be not null automatically, but I've left it in for completeness and in case you later create table as select.)
When using the "Create" code, you must use a comma in the line where you define each column of the table. Except the last column. You can read the oracle sql syntax link as follows: https://docs.oracle.com/cd/E11882_01/server.112/e41085/sqlqr01001.htm#SQLQR110

Creating table via SQL Command Line, invalid identifier

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...

SQL Error: ORA-00922: missing or invalid option

CREATE TABLE chartered flight(flight_no NUMBER(4) PRIMARY KEY
, customer_id NUMBER(6) REFERENCES customer(customer_id)
, aircraft_no NUMBER(4) REFERENCES aircraft(aircraft_no)
, flight_type VARCHAR2 (12)
, flight_date DATE NOT NULL
, flight_time INTERVAL DAY TO SECOND NOT NULL
, takeoff_at CHAR (3) NOT NULL
, destination CHAR (3) NOT NULL)
Should I not use CHAR data type?
I hear it is bad practice to use it but I wanted to make it so takeoff_at and destination have to have minimum 3 characters because they are airport codes.
This is the error I am getting:
Error at Command Line:1 Column:23
Error report:
SQL Error: ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
*Cause:
*Action:
The error you're getting appears to be the result of the fact that there is no underscore between "chartered" and "flight" in the table name. I assume you want something like this where the name of the table is chartered_flight.
CREATE TABLE chartered_flight(flight_no NUMBER(4) PRIMARY KEY
, customer_id NUMBER(6) REFERENCES customer(customer_id)
, aircraft_no NUMBER(4) REFERENCES aircraft(aircraft_no)
, flight_type VARCHAR2 (12)
, flight_date DATE NOT NULL
, flight_time INTERVAL DAY TO SECOND NOT NULL
, takeoff_at CHAR (3) NOT NULL
, destination CHAR (3) NOT NULL)
Generally, there is no benefit to declaring a column as CHAR(3) rather than VARCHAR2(3). Declaring a column as CHAR(3) doesn't force there to be three characters of (useful) data. It just tells Oracle to space-pad data with fewer than three characters to three characters. That is unlikely to be helpful if someone inadvertently enters an incorrect code. Potentially, you could declare the column as VARCHAR2(3) and then add a CHECK constraint that LENGTH(takeoff_at) = 3.
CREATE TABLE chartered_flight(flight_no NUMBER(4) PRIMARY KEY
, customer_id NUMBER(6) REFERENCES customer(customer_id)
, aircraft_no NUMBER(4) REFERENCES aircraft(aircraft_no)
, flight_type VARCHAR2 (12)
, flight_date DATE NOT NULL
, flight_time INTERVAL DAY TO SECOND NOT NULL
, takeoff_at CHAR (3) NOT NULL CHECK( length( takeoff_at ) = 3 )
, destination CHAR (3) NOT NULL CHECK( length( destination ) = 3 )
)
Since both takeoff_at and destination are airport codes, you really ought to have a separate table of valid airport codes and define foreign key constraints between the chartered_flight table and this new airport_code table. That ensures that only valid airport codes are added and makes it much easier in the future if an airport code changes.
And from a naming convention standpoint, since both takeoff_at and destination are airport codes, I would suggest that the names be complementary and indicate that fact. Something like departure_airport_code and arrival_airport_code, for example, would be much more meaningful.
there's nothing wrong with using CHAR like that..
I think your problem is that you have a space in your tablename. It should be: charteredflight or chartered_flight..
You should not use space character while naming database objects. Even though it's possible by using double quotes(quoted identifiers), CREATE TABLE "chartered flight" ..., it's not recommended. Take a closer look here