oracle sql:无法将外键添加到表->不合法的识别符? [英] oracle sql: not able to add foreign key to table -> invalid identifier?

查看:191
本文介绍了oracle sql:无法将外键添加到表->不合法的识别符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是db和sql的真正新手.但是,我必须对表PERSON和SPECIES进行操作,并且要向表SPECIES添加外键.尝试添加外键时,我总是收到错误消息"ORA-900904:无效标识符".我只是不知道自己做错了什么,为什么它不起作用?!?!!

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?!?!

这是我的方法:

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表和主键

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:

用于指代PERSON的SPECIES外键

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

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

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

推荐答案

您引用的是persid,它不是表species中的列,因此出现错误...

you are refereing to persid which is not a column in table species thus the error...

编辑-根据评论:

这意味着您需要species中的某些列用作外键...如果没有这样的列,则需要先建立一个列,然后才能创建该约束.像这样:

It means that you need some column in species to 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:

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

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

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

这篇关于oracle sql:无法将外键添加到表->不合法的识别符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆