Oracle SQL Database error: "no matching unique or primary key for this column-list"
Oracle SQL Database error: "no matching unique or primary key for this column-list"
我正在尝试在 Oracle sql 开发人员中设置数据库,我有这 3 个 table。
我需要 table "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
谢谢!
引用的 table 的主键中的每一列不需要单独的外键 - 一个外键中可以有多个列,例如:
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
table 还有其他列?因为就目前而言,地址 table 毫无意义,可以跳过!
我正在尝试在 Oracle sql 开发人员中设置数据库,我有这 3 个 table。 我需要 table "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
谢谢!
引用的 table 的主键中的每一列不需要单独的外键 - 一个外键中可以有多个列,例如:
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
table 还有其他列?因为就目前而言,地址 table 毫无意义,可以跳过!