ORA-00907: Missing Right Parenthesis On Creating Foreign Key Oracle 12c

筅森魡賤 提交于 2020-11-29 09:28:25

问题


I Want To Make a Table Which Include One Auto Generated Primary Key And Two Foreign Keys But I'm Facing This Error...

create table answers
( id number generated by default on null as identity primary key
, question_id number foreign key references questions(id)
, user_id number foreign key references users(id)
, answer varchar(1000)
, post_date date);

create table answers (id number generated by default on null as identity primary key, question_id number foreign key references questions(id), user_id number foreign key references users(id), answer varchar(1000), post_date date)

ERROR at line 1: ORA-00907: missing right parenthesis

Then, I tried this:

create table answers
( id number generated by default on null as identity primary key
, question_id number foreign key (question_id) references questions(id)
, user_id number foreign key (user_id) references users(id)
, answer varchar(1000)
, post_date date );

create table answers
( id number generated by default on null as identity primary key
, question_id number foreign key (question_id) references questions(id)
, user_id number foreign key (user_id) references users(id)
, answer varchar(1000)
, post_date date )
                                                                                                     *

ERROR at line 1: ORA-00907: missing right parenthesis

It is still Giving Same Error!

But, if I do it in two steps:

create table answers
( id number generated by default on null as identity primary key
, question_id number not null
, user_id number not null
, answer varchar(1000)
, post_date date );

Table created.

alter table answers
add foreign key (question_id) references questions(id) add foreign key (user_id) references users(id);

Table altered.

Can anyone please get me out of this?


回答1:


When defining the constraint in-line as part of the column definition you don't need to say foreign key:

create table answers
( id number generated by default on null as identity primary key
, question_id number references questions(id)
, user_id number references users(id)
, answer varchar(1000)
, post_date date);

Best of luck.




回答2:


you may define foreign keys at the bottom, non-adjacent to column names, like below :

create table answers (
                      id number generated by default on null as identity primary key, 
                      question_id number, 
                      user_id number, 
                      answer varchar(1000), 
                      post_date date,
                      foreign key(question_id) references questions(id),
                      foreign key(user_id) references users(id)                            
                     );


来源:https://stackoverflow.com/questions/48045336/ora-00907-missing-right-parenthesis-on-creating-foreign-key-oracle-12c

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