【问题标题】:Oracle SQL Database error: "no matching unique or primary key for this column-list"Oracle SQL 数据库错误:“此列列表没有匹配的唯一键或主键”
【发布时间】:2016-03-29 15:36:23
【问题描述】:

我正在尝试在 Oracle sql developer 中建立一个数据库,我有这 3 个表。 我需要表“GuyAddress”有 3 个主键,它们也是外键。这就是我遇到一个我无法理解的错误的地方。

CREATE TABLE Guy
( 
    id NUMBER(10)   PRIMARY KEY, 
    name            VARCHAR(50)
);

CREATE TABLE Address
(
    zipcode         VARCHAR(6),
    "number"        NUMBER(10),
    CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);

CREATE TABLE GuyAddress
(
    Guy_id          NUMBER(10),
    Address_zipcode VARCHAR(6),
    Address_number  NUMBER(10),
    CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
    CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
    CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
    CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);

这是错误,希望有人能发现我犯的错误,因为我不能...

Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
  (
    Guy_id          NUMBER(10),
    Address_zipcode VARCHAR(6),
    Address_number  NUMBER(10),
    CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
    CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
    CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
    CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
  )
Error report -
SQL Error: 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

谢谢!

【问题讨论】:

  • CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number") 这是组合主键,而您使用的是CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),,这可能是原因。
  • 这是针对 Oracle(根据您的帖子标题和问题中的错误消息)还是 MySQL?你已经标记了两者。
  • 对不起,标签是“sql”。

标签: sql oracle foreign-keys key constraints


【解决方案1】:

对于被引用表的主键中的每一列,您不需要单独的外键 - 您可以在一个外键中有多个列,例如:

CREATE TABLE Guy
( 
    id NUMBER(10)   PRIMARY KEY, 
    name            VARCHAR(50)
);

CREATE TABLE Address
(
    zipcode         VARCHAR(6),
    address_number  NUMBER(10),
    CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);

CREATE TABLE GuyAddress
(
    Guy_id          NUMBER(10),
    Address_zipcode VARCHAR(6),
    Address_number  NUMBER(10),
    CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
    CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
    CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);

请注意,我已将您的 address.number 列更新为 address.address_number,因为不建议您使用基于关键字的列。也不建议使用双引号来解决这个问题(并强制区分大小写);您必须记住每次引用该列时都使用它们!

顺便说一句,我假设您的address 表还有其他列?因为就目前而言,地址表是没有意义的,可以跳过!

【讨论】:

  • 现在我收到此错误:从第 18 行开始的错误命令 - 错误报告 - SQL 错误:ORA-02264:名称已被现有约束 02264 使用。00000 -“名称已使用通过现有约束” *原因:指定的约束名称必须是唯一的。 *Action:为约束指定一个唯一的约束名称。
  • 您需要更改约束,使它们的名称与预先存在的约束不同。
  • 即使我使用“DROP TABLE GuyAddress CASCADE CONSTRAINTS;”一开始?
  • 没关系,因为我之前使用了引号或破折号,所以它没有删除表格。非常感谢您的帮助!
  • 没问题。您已经很好地证明了为什么使用引号通常是一个坏主意!如果您发现自己在标识符周围使用双引号,请花点时间看看是否有替代方法可以避免它们!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多