如何在SQL中修复ORA-02270错误代码?

问题描述 投票:1回答:1
CREATE TABLE employee (
      empid        INT PRIMARY KEY,
      fname        VARCHAR2(50),
      lname        VARCHAR2(50)
);

CREATE TABLE manager (
      mgrid   INT
            REFERENCES employee ( empid )
);

CREATE TABLE barista (
      baristaid  INT
            REFERENCES employee ( empid ),
      mgrid      INT
            REFERENCES manager ( mgrid )
);

对于咖啡师桌,它给我一个错误,

ORA-02270: no matching unique or primary key for this column-list
02270. 00000 -  "no matching unique or primary key for this column-list"
*Cause:    A REFERENCES clause in a CREATE/ALTER TABLE statement
           gives a column-list for which there is no matching unique or primary
           key constraint in the referenced table.
*Action:   Find the correct column names using the ALL_CONS_COLUMNS
           catalog view

我也尝试过,

ALTER TABLE barista
      ADD CONSTRAINT fk_mgrid FOREIGN KEY ( mgrid )
            REFERENCES manager ( mgrid );

但是我遇到了同样的错误。你能告诉我出什么事了吗?谢谢你。

sql oracle foreign-keys oracle10g create-table
1个回答
2
投票

您的直接问题是,应将manager(mgrid)声明为主键(或至少是唯一键),以便可以用外键引用它。

所以您需要更改此:

CREATE TABLE manager (
      mgrid   INT 
            REFERENCES employee ( empid )
);

收件人:

CREATE TABLE manager (
      mgrid   INT primary key
            REFERENCES employee ( empid )
);

更改后,您的代码just works

© www.soinside.com 2019 - 2024. All rights reserved.