简体   繁体   English

外键sql oracle

[英]foreign key sql oracle

i'm having trouble adding a foreign key to my table. 我在向表中添加外键时遇到问题。 Any suggestions would be amazing, thanks :) the errors i am getting are below 任何建议都将是惊人的,谢谢:)我得到的错误如下

DROP TABLE Table_One
/

CREATE TABLE Table_One
(
col1 number(10),
col2 varchar(200),
PRIMARY KEY(col1)
)

/
DROP TABLE Table_Two
/

CREATE TABLE Table_Two
(
col3 number(10), 
col2 varchar(200),
PRIMARY KEY(col3),
CONSTRAINT fk_col2 FOREIGN KEY(col2) references Table_One(col2)
)
/

SQL> start newtest.sql

Table dropped.


Table created.

DROP TABLE Table_Two * ERROR at line 1: ORA-00942: table or view does not exist DROP TABLE Table_Two *第1行出现错误:ORA-00942:表或视图不存在

CONSTRAINT fk_col2 FOREIGN KEY(col2) references Table_One(col2)
                                                      *

ERROR at line 6: ORA-02270: no matching unique or primary key for this column-list 第6行出现错误:ORA-02270:此列列表没有匹配的唯一键或主键

The error on the DROP TABLE is because the table doesn't exist yet. DROP TABLE上的错误是因为该表尚不存在。 The second error is because you have defined col1 as the primary key of Table_One , not col2 , and a foreign key can only reference a primary or unique key. 第二个错误是因为您已将col1定义为Table_One的主键,而不是col2 ,并且外键只能引用主键或唯一键。 So, either you need to reference col1 or you need to create a unique key on col2 . 因此,您需要引用col1或需要在col2上创建unique key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM