简体   繁体   中英

Cant create a foreign key for a table that has two primary keys set

I am trying to create a foreign key pointed from one table to another. these being tbl_inventory and tbl_player . The player table has two primary keys set, player_score and playerID . When I try to create a foreign key within the inventory table to get the playerID. it throws the error below: 在此处输入图像描述

Here is my SQL script:

drop database if exists example_db;
create database example_db;
use example_db;


/*Player  ===================================================================================*/
drop table if exists tbl_player; 
CREATE TABLE `tbl_player` (
    `player_score` INTEGER DEFAULT 0 NOT NULL,
    `playerID` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10375 PRIMARY KEY (`player_score`,`playerID`)
);

/*Inventory ===================================================================================*/
drop table if exists tbl_inventory;
CREATE TABLE `tbl_inventory` (
    `inventoryID` INTEGER NOT NULL,
    `playerID` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10520 PRIMARY KEY (`inventoryID`),
        FOREIGN KEY (playerID) REFERENCES tbl_player(playerID)
);

I did some troubleshooting and found that it works when I only define one primary key, but two are required for the project that I am working on. Does anybody know how to fix this error?

You have a composite primary key, so you need a composite foreign key:

/*Inventory ===================================================================================*/
drop table if exists tbl_inventory;
CREATE TABLE `tbl_inventory` (
    `inventoryID` INTEGER NOT NULL,
    `playerID` INTEGER DEFAULT 0 NOT NULL,
    `player_score` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10520 PRIMARY KEY (`inventoryID`),
    FOREIGN KEY (player_score,playerID) REFERENCES tbl_player(player_score,playerID)
);

Also you can may be try having one primary key and the other column as unique: (only if it works for your project to have single column of table as primary and other as unique)

/*Player  ===================================================================================*/
drop table if exists tbl_player; 
CREATE TABLE `tbl_player` (
    `player_score` INTEGER DEFAULT 0 NOT NULL,
    `playerID` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10375 PRIMARY KEY (`playerID`),
    UNIQUE(`player_score`)
);

/*Inventory ===================================================================================*/
drop table if exists tbl_inventory;
CREATE TABLE `tbl_inventory` (
    `inventoryID` INTEGER NOT NULL,
    `playerID` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10520 PRIMARY KEY (`inventoryID`),
    FOREIGN KEY (playerID) REFERENCES tbl_player(playerID)
);

Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM