SQL 将 FOREIGN KEY 设置为非唯一值会在 sql 开发人员中引发错误
SQL set FOREIGN KEY to a non unique value throws error in sql developer
我遇到了 N:M 的情况,但我不能将 2 个非唯一值中的任何一个用作外键,谁能帮忙?? Platform_Id 和 Station_Id 都不能在 Platform_Host table.
中设置为外键
CREATE TABLE Repair_Platform
(
Platform_Id INT NOT NULL,
Station_Id INT NOT NULL,
Mechanic_Id INT NOT NULL UNIQUE,
Validation_Num INT NOT NULL,
CONSTRAINT Platform_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Station (Station_Id),
CONSTRAINT Platform_fk2
FOREIGN KEY (Mechanic_Id) REFERENCES Engineer (Engineer_Id),
CONSTRAINT Platform_pk
PRIMARY KEY (Station_Id, Platform_Id)
);
CREATE TABLE Platform_Host
(
Station_Id INT NOT NULL,
Platform_Id INT NOT NULL,
Vehicle_Id VARCHAR(8) NOT NULL,
DateTime DATE NOT NULL,
CONSTRAINT Host_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Platform (Station_Id),
CONSTRAINT Host_fk2
FOREIGN KEY (Vehicle_Id) REFERENCES Vehicle (License_Plate),
CONSTRAINT Host_fk3
FOREIGN KEY (Platform_Id) REFERENCES Repair_Platform (Platform_Id)
);
错误:
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
您需要引用 Repair_Platform 的整个主键 - 如下所示:
CREATE TABLE Platform_Host
(
-- columns
-- constraints
CONSTRAINT Host_fk3
FOREIGN KEY (StationId, Platform_Id)
REFERENCES Repair_Platform (StationId, Platform_Id)
);
您不能仅引用主键的一部分 - 这是一个全有或全无交易。
我遇到了 N:M 的情况,但我不能将 2 个非唯一值中的任何一个用作外键,谁能帮忙?? Platform_Id 和 Station_Id 都不能在 Platform_Host table.
中设置为外键CREATE TABLE Repair_Platform
(
Platform_Id INT NOT NULL,
Station_Id INT NOT NULL,
Mechanic_Id INT NOT NULL UNIQUE,
Validation_Num INT NOT NULL,
CONSTRAINT Platform_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Station (Station_Id),
CONSTRAINT Platform_fk2
FOREIGN KEY (Mechanic_Id) REFERENCES Engineer (Engineer_Id),
CONSTRAINT Platform_pk
PRIMARY KEY (Station_Id, Platform_Id)
);
CREATE TABLE Platform_Host
(
Station_Id INT NOT NULL,
Platform_Id INT NOT NULL,
Vehicle_Id VARCHAR(8) NOT NULL,
DateTime DATE NOT NULL,
CONSTRAINT Host_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Platform (Station_Id),
CONSTRAINT Host_fk2
FOREIGN KEY (Vehicle_Id) REFERENCES Vehicle (License_Plate),
CONSTRAINT Host_fk3
FOREIGN KEY (Platform_Id) REFERENCES Repair_Platform (Platform_Id)
);
错误:
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
您需要引用 Repair_Platform 的整个主键 - 如下所示:
CREATE TABLE Platform_Host
(
-- columns
-- constraints
CONSTRAINT Host_fk3
FOREIGN KEY (StationId, Platform_Id)
REFERENCES Repair_Platform (StationId, Platform_Id)
);
您不能仅引用主键的一部分 - 这是一个全有或全无交易。