2
votes

I am using Oracle SQL developer to create a basic table with the following command:

CREATE TABLE chartered_flight(
   flight_no NUMBER(4) PRIMARY KEY
   , customer_id FOREIGN KEY
   , aircraft_no FOREIGN KEY
   , flight_type VARCHAR2 (12)
   , flight_date DATE NOT NULL
   , flight_time TO_DATE 'HH:MI' NOT NULL
   , takeoff_at CHAR (3) NOT NULL
   , destination CHAR (3) NOT NULL
)

Where is the missing right parenthesis? Or is the syntax that I have used incorrect.

I have made the following changes:

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)

Now I get this error:

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:

I have a feeling it is something to do with TO_DATE or is it because I have not created my aircraft table yet so aircraft_no is missing? Can some one please help, thanks.

3
Don't surround table/column names with [ ]. - Jeffrey Kemp

3 Answers

2
votes

There is no type called TO_DATE. To_DATE is used to convert a string to a date.

So the create statement should be something like

CREATE TABLE chartered_flight(
    flight_no NUMBER(4) PRIMARY KEY, 
    customer_id FOREIGN KEY, 
    aircraft_no FOREIGN KEY, 
    flight_type VARCHAR2 (12), 
    flight_date DATE NOT NULL,
    flight_time VARCHAR(4) NOT NULL, 
    takeoff_at CHAR (3) NOT NULL, 
    destination CHAR (3) NOT NULL)

You can use to_date when selecting data from the table. For example

Select to_date(flight_date, 'yyyy/mm/dd') from chartered_flight;
1
votes

To specify foreign key constraint, you should either use inline customer_id [type] REFERENCES [master_table_name]([master_column_name]) or out of line syntax : , CONSTRAINT [constraint_name] FOREIGN KEY(customer_id) REFERENCES [master_table_name]([master_column_name]) See more example here. Also, it usually makes sense to add indexes on foreign key columns.

For flight_time you probably need to use INTERVAL DAY TO SECOND type

0
votes

ALTER TABLE EMPLOYEE_DETAILS ADD ( EMPLOYEE_ID NUMBER(10), EMPLOYEE NAME VARCHAR2(100), );

Above ALTER statement caused the below error.
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:

How ever I have rectified my self and here is the correct one.

ALTER TABLE EMPLOYEE_DETAILS ADD ( EMPLOYEE_ID NUMBER(10), EMPLOYEE_NAME VARCHAR2(100), );

Solution: There is a space in the name of second column which caused this error.

Thanks,
Manne