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
Related
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
);
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'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
This is the query I am trying to execute against an Oracle database which apparently has no parenthesis missing.
CREATE TABLE P_DOG(
DOG_ID CHAR(5) NOT NULL,
DOG_NAME VARCHAR2(30) NOT NULL DEFAULT 'UNKNOWN',
DOG_BIRTHDAY_MONTH NUMBER(2) NULL CHECK(DOG_BIRTH_MONTH>=1 AND DOG_BIRTH_MONTH<=12),
DOG_BIRTHDAY_YEAR NUMBER(4) NOT NULL CHECK(DOG_BIRTH_YEAR>=1980 AND DOG_BIRTH_YEAR<= 2030),
SEX CHAR(1) NOT NULL,
SPAYED_OR_NEUTERED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT DOG_PK PRIMARY KEY(DOG_ID),
);
Besides the extra comma at the end that Mat pointed out in a comment, NOT NULL should come after the defaults - and the check constraints should have the correct column names: the column names have BIRTHDAY but the conditions use BIRTH, change either the column name or what you have in the conditions so they match.
Specifically the "missing right parenthesis" error is caused by having a DEFAULT after NOT NULL. Wrong order.
This question already has an answer here:
ORA 00918- Column ambiguosly defined error [duplicate]
(1 answer)
Closed 9 years ago.
I am trying to retrieve some data (coursename) from one of my tables but the following error is coming all the time
ORA-00918: column ambiguously defined
the command I am typing is:
select bookno,courno,coursename
from booking, course,coursename
where bookno = 6200
and booking.courno = course.courno
and coursename.coursenameno = course.coursenameno
I have some tables as described :
CREATE TABLE BOOKING
(BOOKNO NUMBER (4) NOT NULL,
COURNO NUMBER (4) NOT NULL,
BOOKDATE DATE,
BOOKCUSTPAYMENT VARCHAR (20),
CONSTRAINT PK_BOOK PRIMARY KEY (BOOKNO,COURNO),
CONSTRAINT FK_BOOK FOREIGN KEY (COURNO) REFERENCES COURSE(COURNO)
ON DELETE CASCADE);
CREATE TABLE CUSTOMER
(CUSTNO NUMBER (4) NOT NULL, -- creation of primary-key
PROFNO NUMBER (4) NOT NULL,
CUSTFNAME VARCHAR (15),
CUSTLNAME VARCHAR (15),
CUSTDOB DATE,
CUSTPHONEDAY NUMBER (15),
CUSTPHONEEVE NUMBER (15),
CONSTRAINT PK_CUST PRIMARY KEY (CUSTNO),
CONSTRAINT FK_PROF FOREIGN KEY (PROFNO) REFERENCES PROFICIENCY(PROFNO)
ON DELETE CASCADE);
CREATE TABLE COURSENAME
( COURSENAMENO NUMBER (4) NOT NULL,
COURSENAME VARCHAR (20),
COURSEDESC VARCHAR (120),
COURSEDAYCOST NUMBER (7,2),
CONSTRAINT PK_COURSENAME PRIMARY KEY (COURSENAMENO));
CREATE TABLE COURSE
(COURNO NUMBER (4) NOT NULL, -- creation of primary-key
COURSTART DATE,
COUREND DATE,
COURSENAMENO NUMBER (4) NOT NULL,
ACCDAYNO NUMBER (4) NOT NULL,
FOODNO NUMBER (4) NOT NULL,
TRANSNO NUMBER (4) NOT NULL,
CONSTRAINT PK_COURSE PRIMARY KEY (COURNO),
CONSTRAINT FK_COURSENAME FOREIGN KEY (COURSENAMENO) REFERENCES COURSENAME(COURSENAMENO));
I am researching but I cannot figure out what is happening !!!
when the same column appears in several tables you need to specify which table is the one to be used. As a general rulem its always a good idea to prefix the column with the table (or alias) as improves readability and speeds up parsing.
so, for your query try (changes in upper case)
select BOOKING.bookno,BOOKING.courno,COURSENAME.coursename
from booking, course,coursename
where BOOKING.bookno = 6200
and booking.courno = course.courno
and coursename.coursenameno = course.coursenameno
You need to specify from which table the columns in SELECT and WHERE statements should be retrieved:
select booking.bookno, booking.courno, course.coursename
from booking, course, coursename
where booking.bookno = 6200
and booking.courno = course.courno
and coursename.coursenameno = course.coursenameno
Also, consider using ANSI SQL-92+ JOIN syntax like so:
select booking.bookno, booking.courno, course.coursename
from booking
inner join course on booking.courno = course.courno
inner join coursename on coursename.coursenameno = course.coursenameno
where booking.bookno = 6200
See [Bad habits to kick : using old-style JOINs][1] for some reasoning about it.
[1]: https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins
When a column is ambigious, this means the database doesnt know which column to use from 2 or more different tables.
You must define in the select like this
select tablename.bookno,tablename.courno,tablename.coursename
from booking, course,coursename
where tablename.bookno = 6200
and booking.courno = course.courno <-- Here its correct
and coursename.coursenameno = course.coursenameno <-- Here its correct
Change tablename. to the correct table where the column is.
field courno in your select: you haven't defined from which table: course or booking