oracle sql:无法将外键添加到表 -> 标识符无效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8119438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:26:19  来源:igfitidea点击:

oracle sql: not able to add foreign key to table -> invalid identifier?

sqloracleforeign-keysforeign-key-relationship

提问by samjam

First off, I'm a real newbie to db and sql. However, I have to tables, PERSON and SPECIES and I want to add a foreign key to the table SPECIES. When trying to add the foreign key I always get the error message "ORA-900904: invalid identifier". I just can't figure out what I've done wrong and why it doesn't work?!?!

首先,我是 db 和 sql 的真正新手。但是,我必须有表、PERSON 和 SPECIES,并且我想向表 SPECIES 添加一个外键。尝试添加外键时,我总是收到错误消息“ORA-900904:无效标识符”。我就是不知道我做错了什么,为什么它不起作用?!?!

This was my approach:

这是我的方法:

PERSON table and primary key

PERSON 表和主键

create table person
(
name varchar2 (30),
firstname varchar2 (30),
persid number (8) not null
)
;

alter table person 
add constraint person_pk 
primary key (persid)
;

SPECIES table and primary key

SPECIES 表和主键

create table species
(
speciesnamelat varchar2 (30),
vartid number (8) not null
)
;

alter table Species
add constraint species_pk
primary key (vartid)
;

This part worked fine but the following didn't work:

这部分工作正常,但以下不起作用:

Foreign Key for SPECIES referring to PERSON

指代人的物种的外键

alter table species
add constraint species_person_fk
foreign key (persid)
references person (persid)
;

I always get this Error "ORA-900904: invalid identifier"

我总是收到此错误“ORA-900904:无效标识符”

回答by Yahia

you are refereing to persidwhich is not a column in table speciesthus the error...

您所指的persid不是表中的列,species因此错误...

EDIT - As per comments:

编辑 - 根据评论:

It means that you need some column in speciesto be used as foreign key... if there is no such column then you need to build one in before you can create that constraint. Like this:

这意味着您需要将某些列species用作外键……如果没有这样的列,那么您需要先构建一个,然后才能创建该约束。像这样:

alter table species
add persid number(8) not null
;
alter table species
add constraint species_person_fk
foreign key (persid)
references person (persid)
;

Depending on your data model, SPECIES.PERSID may be optional or mandatory.

根据您的数据模型,SPECIES.PERSID 可能是可选的或强制性的。