SQL Error: ORA-00907: missing right parenthesis

旧巷老猫 提交于 2019-12-02 10:17:40

问题


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.


回答1:


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




回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/8376662/sql-error-ora-00907-missing-right-parenthesis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!