简体   繁体   English

为什么在我尝试创建外键时会出现错误提示该列不存在

[英]Why does it come up with an error that says that the column doesnt exist when I try and create foreign keys

I am relatively new to mysql and writing queries.我对 mysql 和编写查询比较陌生。 I am trying to create a database that has multiple different relationships.我正在尝试创建一个具有多种不同关系的数据库。 I am able to create the tables and assign unique indexes, but when I try and alter each table, I get the same error depending on what Foreign Key I am trying to create.我能够创建表并分配唯一索引,但是当我尝试更改每个表时,我会收到相同的错误,具体取决于我尝试创建的外键。 Here is the error I am getting:这是我得到的错误:

在此处输入图像描述

Error Code: 1072. Key column 'gameID' doesn't exist in table错误代码:1072。表中不存在键列“gameID”

This is when I try and call这是我尝试打电话的时候

ALTER TABLE `tbl_player` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`);

Below Is the current SQL script:以下是当前的 SQL 脚本:

drop database if exists test_DB;
create database test_DB;
use test_DB;

drop procedure if exists createdb;  
delimiter //
create procedure createdb()
begin

/*Leaderboard*/
drop table if exists tbl_leaderboard;   
CREATE TABLE `tbl_leaderboard` (
    `leaderboardID` INTEGER NOT NULL,
    `totalScore` INTEGER DEFAULT 0 NOT NULL,
    `gamesPlayed` INTEGER DEFAULT 0 NOT NULL,
    `averageScore` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10503 PRIMARY KEY (`leaderboardID`)
);

/*User ===================================================================================*/ 
drop table if exists tbl_user; 
CREATE TABLE `tbl_user` (
    `userID` INTEGER NOT NULL,
    `username` VARCHAR(255) NOT NULL,
    `user_password` VARCHAR(20) NOT NULL,
    `user_email` VARCHAR(20) NOT NULL,
    `user_loginAttermpts` INTEGER DEFAULT 0 NOT NULL,
    `user_accountStatus` INTEGER DEFAULT 0 NOT NULL,
    `user_isAdmin` BOOLEAN NOT NULL,
    CONSTRAINT SYS_PK_10513 PRIMARY KEY (`userID`,`username`)
);

/*Player  ===================================================================================*/
drop table if exists tbl_player; 
CREATE TABLE `tbl_player` (
    `player_score` SMALLINT DEFAULT 0 NOT NULL,
    `playerID` SMALLINT NOT NULL,
    `colour` VARCHAR(20) 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,
    `quantity` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10520 PRIMARY KEY (`inventoryID`)
);

/*Tile ===================================================================================*/
drop table if exists tbl_tile;
CREATE TABLE `tbl_tile` (
    `tileID` INTEGER NOT NULL,
    CONSTRAINT SYS_PK_10547 PRIMARY KEY (`tileID`)
    
);

/*Board ===================================================================================*/
drop table if exists tbl_board;
CREATE TABLE `tbl_board` (
    `boardID` INTEGER NOT NULL,
    CONSTRAINT SYS_PK_10538 PRIMARY KEY (`boardID`)
    
);

/*Game ===================================================================================*/
drop table if exists tbl_game;
CREATE TABLE `tbl_game` (
    `gameID` INTEGER NOT NULL,
    `gameNumber` INTEGER DEFAULT 0 NOT NULL,
    `numberOfPlayers` INTEGER DEFAULT 0 NOT NULL,
    `gameMode` VARCHAR(30) NOT NULL,
    CONSTRAINT SYS_PK_10543 PRIMARY KEY (`gameID`)
);

/*Chat ===================================================================================*/
drop table if exists tbl_gameChat;
CREATE TABLE `tbl_gameChat` (
    `chatID` SMALLINT NOT NULL,
    `text` VARCHAR(255),
    CONSTRAINT SYS_PK_10555 PRIMARY KEY (`chatID`)
);

/*TileAsset ===================================================================================*/
drop table if exists tbl_assetTile;
CREATE TABLE `tbl_assetTile` (
    `assetTileID` INTEGER NOT NULL,
    CONSTRAINT SYS_PK_10545 PRIMARY KEY (`assetTileID`)
);

/*Asset ===================================================================================*/
drop table if exists tbl_asset;
CREATE TABLE `tbl_asset` (
    `assetID` INTEGER NOT NULL,
    `asset_name` VARCHAR(50) NOT NULL,
    `asset_type` VARCHAR(50) NOT NULL,
    `asset_value` INTEGER DEFAULT 0 NOT NULL,
    CONSTRAINT SYS_PK_10557 PRIMARY KEY (`assetID`)
);

/*Create UNIQUE Indexes*/
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10503_10504 ON `tbl_leaderboard` (`leaderboardID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10497_10498 ON `tbl_user` (`userID`,`username`);
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10513_10514 ON `tbl_user` (`userID`,`username`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10375_10376 ON `tbl_player` (`player_score`,`playerID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10520_10521 ON `tbl_inventory` (`inventoryID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10547_10548 ON `tbl_tile` (`tileID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10538_10539 ON `tbl_board` (`boardID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10543_10544 ON `tbl_game` (`gameID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10555_10556 ON `tbl_gameChat` (`chatID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10545_10546 ON `tbl_assetTile` (`assetTileID`);
    
    CREATE UNIQUE INDEX SYS_IDX_SYS_PK_10557_10558 ON `tbl_asset` (`assetID`);


/*Add Foreign Keys*/
    ALTER TABLE `tbl_player` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`);
    ALTER TABLE `tbl_player` ADD FOREIGN KEY (`tileID`) REFERENCES tbl_tile(`tileID`);
    ALTER TABLE `tbl_player` ADD FOREIGN KEY (`userID`) REFERENCES tbl_user(`userID`);
    
    ALTER TABLE `tbl_inventory` ADD FOREIGN KEY (`playerID`) REFERENCES tbl_player(`playerID`);
    ALTER TABLE `tbl_inventory` ADD FOREIGN KEY (`assetID`) REFERENCES tbl_asset(`assetID`);
    
    ALTER TABLE `tbl_tile` ADD FOREIGN KEY (`boardID`) REFERENCES tbl_board(`boardID`);
    
    ALTER TABLE `tbl_board` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`);
    
    ALTER TABLE `tbl_gameChat` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`);
    
    ALTER TABLE `tbl_assetTile` ADD FOREIGN KEY (`tileID`) REFERENCES tbl_tile(`tileID`);
    ALTER TABLE `tbl_assetTile` ADD FOREIGN KEY (`assetID`) REFERENCES tbl_asset(`assetID`);

    ALTER TABLE `tbl_leaderboard` ADD FOREIGN KEY (`player_score`) REFERENCES tbl_player(`player_score`);
    ALTER TABLE `tbl_leaderboard` ADD FOREIGN KEY (`userID`) REFERENCES tbl_user(`userID`);
    ALTER TABLE `tbl_leaderboard` ADD FOREIGN KEY (`username`) REFERENCES tbl_user(`username`);

    
end //

delimiter ;
call createdb();

Any suggestions on how to fix this issue would be greatly appreciated and possible improvements to the fomrmat.任何有关如何解决此问题的建议将不胜感激,并可能对格式进行改进。

Your table tbl_player doesn't have a column gameID add it in you create script to add the FOREIGN KEY , the same applies to all foreign keys you are trying to add.您的表tbl_player没有列gameID在您创建脚本中添加它以添加FOREIGN KEY ,这同样适用于您尝试添加的所有外键。 the column must be available in the table where you are willing to create the FOREIGN KEY该列必须在您愿意创建FOREIGN KEY的表中可用

CREATE TABLE `tbl_player` (
    `player_score` SMALLINT DEFAULT 0 NOT NULL,
    `playerID` SMALLINT NOT NULL,
    `colour` VARCHAR(20) NOT NULL,
    `gameID` INTEGER,
    CONSTRAINT SYS_PK_10375 PRIMARY KEY (`player_score`,`playerID`)
);

Before creating foreign keys for any table, make sure the table contains/has the column you are adding foreign key constraint to.在为任何表创建外键之前,请确保该表包含/具有您要添加外键约束的列。

In your case if you see the table tbl_player (as you have shared)在您的情况下,如果您看到表tbl_player (正如您所共享的)

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

does not have column gameID and the query you are running ALTER TABLE `tbl_player` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`);没有列gameID并且您正在运行的查询ALTER TABLE `tbl_player` ADD FOREIGN KEY (`gameID`) REFERENCES tbl_game(`gameID`); is trying to add Foreign key constraint on column gameID (which does not exists in the tbl_player table) thereby resulting into an error you mentioned Error Code: 1072. Key column 'gameID' doesn't exist in table正在尝试在gameIDtbl_player表中不存在)上添加外键约束,从而导致您提到的Error Code: 1072. Key column 'gameID' doesn't exist in table

To solve this you can:要解决此问题,您可以:

  1. Alter table to add a new column and then add foreign key constraint to that column:更改表以添加新列,然后向该列添加外键约束:
ALTER TABLE tbl_player
  ADD COLUMN gameID INT,
  ADD FOREIGN KEY fk_name(gameID) REFERENCES tbl_game(gameID) ON DELETE CASCADE;
  1. Add a column gameID while creating the table as below:在创建表时添加一列gameID ,如下所示:
/*Player  ===================================================================================*/
drop table if exists tbl_player; 
CREATE TABLE `tbl_player` (
    `player_score` SMALLINT DEFAULT 0 NOT NULL,
    `playerID` SMALLINT NOT NULL,
    `colour` VARCHAR(20) NOT NULL,
    `gameID` INTEGER NOT NULL,
    CONSTRAINT SYS_PK_10375 PRIMARY KEY (`player_score`,`playerID`)
);

then alter the table to create column gameID as foreign key:然后更改表以创建列gameID作为外键:

ALTER TABLE tbl_player add FOREIGN KEY(gameID) REFERENCES tbl_game(gameID);

Any one of the above points should work.以上任何一点都应该有效。

Cheers!!干杯!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试添加外键时 MYSQL 错误? - Why does MYSQL error when I try to add a foreign key? 当我尝试在 Invoice 表上添加外键时,为什么会出现此错误? - Why am i getting this error when i try to add foreign keys on Invoice table? 当我尝试在多对多关联表上设置2个外键时,为什么会收到此错误消息? “外键约束的格式不正确” - Why I obtain this error message when I try to set 2 foreign keys on a many to many association table? “Foreign key constraint is incorrectly formed” 为什么我要获取列名不存在表名称错误 - Why am i getting a column doesnt exist error with the name of the table 无法从MySQL删除记录。 错误表明列不存在,但显然存在 - Cannot delete record from MySQL. Error says column does not exist, when it clearly does 添加 FOREIGN KEY 但说该列不存在? - Adding a FOREIGN KEY but says the column does not exists when it does? 列在那里,但是当我尝试删除它时,它说 MYSQL 中没有列? **错误代码:1091。无法删除……** - Column is there, but when I try to delete it says no column in MYSQL? **Error Code: 1091. Can't DROP…** 尝试创建此表时为什么会出现此FK错误? errno:150“外键约束格式不正确 - Why I obtain this FK error when I try to create this table? errno: 150 "Foreign key constraint is incorrectly formed 为什么当我尝试基于同一列进行排序两次时 SQL 不会抛出错误? - Why SQL does not throw error when I try to sort based on same column twice? Laravel 在创建外键时抛出一般错误:1215 无法添加外键约束” - Laravel throws General error: 1215 Cannot add foreign key constraint" when I create foreign keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM