[英]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
正在尝试在
gameID
列( tbl_player表中不存在)上添加外键约束,从而导致您提到的Error Code: 1072. Key column 'gameID' doesn't exist in table
To solve this you can:要解决此问题,您可以:
ALTER TABLE tbl_player
ADD COLUMN gameID INT,
ADD FOREIGN KEY fk_name(gameID) REFERENCES tbl_game(gameID) ON DELETE CASCADE;
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.