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

How can I fix ORA-02270 error code in SQL?

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 )
);

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

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 );

但是我得到了同样的错误。 你能告诉我出了什么问题吗? 提前谢谢你。

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

所以你需要改变这个:

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

收件人:

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

通过此更改,您的代码 just works