ORA-00907: 创建外键 Oracle 12c 时缺少右括号

2022-01-20 00:00:00 sql foreign-keys oracle ddl sqlplus

我想制作一个包含一个自动生成的主键和两个外键的表,但我遇到了这个错误......

创建表格答案(默认在null上生成的id号作为身份主键, question_id number 外键引用问题(id), user_id number 外键引用 users(id), 回答 varchar(1000), post_date 日期);创建表答案(默认在 null 上生成的 ID 号作为身份主键,question_id 号外键引用问题(id),user_id 号外键引用用户(id),答案 varchar(1000),post_date 日期)

<块引用>

第 1 行的错误:ORA-00907: 缺少右括号

然后,我尝试了这个:

创建表格答案(默认在null上生成的id号作为身份主键, question_id 编号外键 (question_id) 引用问题(id), user_id number 外键(user_id) 引用users(id), 回答 varchar(1000), post_date 日期);创建表答案(默认在null上生成的id号作为身份主键, question_id 编号外键 (question_id) 引用问题(id), user_id number 外键(user_id) 引用users(id), 回答 varchar(1000), post_date 日期)*

<块引用>

第 1 行的错误:ORA-00907: 缺少右括号

仍然报同样的错误!

但是,如果我分两步进行:

创建表格答案(默认在null上生成的id号作为身份主键, question_id 编号不为空, user_id 编号不为空, 回答 varchar(1000), post_date 日期);表已创建.更改表答案添加外键(question_id)引用问题(id)添加外键(user_id)引用用户(id);

<块引用>

表已更改.

任何人都可以帮我解决这个问题吗?

解决方案

在将内联约束定义为列定义的一部分时,您不需要说 foreign key:

创建表格答案(默认在null上生成的id号作为身份主键, question_id 编号参考问题(id), user_id number 引用 users(id), 回答 varchar(1000), post_date 日期);

祝你好运.

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?

解决方案

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.

相关文章