Reputation: 81
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
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
Upvotes: 0
Views: 56
Reputation: 8386
The error on the DROP TABLE
is because the table doesn't exist yet. 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. So, either you need to reference col1
or you need to create a unique key
on col2
.
Upvotes: 1