问题
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