I am creating two tables one Platforms table and one game table and I am getting an error saying that it can not find PLATFORMID in sql statment but I don't understand why
Column "PLATFORMID" not found; SQL statement: CREATE TABLE game( gameId LONG PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), publisher VARCHAR(255), platform VARCHAR(255), price DECIMAL(4,2), FOREIGN KEY (platformId) REFERENCES Platforms(platformId) ) [42122-200]
This is my code for creating my tables
CREATE TABLE Platforms(
platformId LONG PRIMARY KEY AUTO_INCREMENT,
platformName VARCHAR(255)
);
CREATE TABLE game(
gameId LONG PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), publisher VARCHAR(255), platform VARCHAR(255), price DECIMAL(4,2),
FOREIGN KEY (platformId) REFERENCES Platforms(platformId)
);
CodePudding user response:
The error is because a platformId
reference is not found in the game
table.
A few things to note:
- The
LONG
data type is an alias forMEDIUMTEXT
, which surely would not haveAUTO_INCREMENT
? I get an error with the use as-is. - When you do
FOREIGN KEY (platformId)
, this portion refers to a column in the current table, and then the other table that this column refers to is the:REFERENCES Platforms(platformId)
portion. - The foreign key's
(column)
reference in the current table must have an index. - It's critical to know that the column reference in BOTH tables for the foreign key must have the same data type, including signed-ness, if specified. (For example, if one column has unsigned, then other column needs to be unsigned too)
So, knowing those things, in the example below:
- I've replaced
LONG
withINT(11) unsigned
in both tables. - Added a column for
platformId
in thegame
table - Adds an index to the
platformId
column in thegame
table as well.
Putting all that together, here's an example interaction:
root@localhost((none)) use test2;
Database changed
root@localhost(test2) show tables;
Empty set (0.00 sec)
root@localhost(test2) CREATE TABLE Platforms(
-> platformId INT(11) unsigned PRIMARY KEY AUTO_INCREMENT,
-> platformName VARCHAR(255)
-> );
Query OK, 0 rows affected (0.21 sec)
root@localhost(test2) CREATE TABLE game(
-> gameId INT(11) unsigned PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(255),
-> publisher VARCHAR(255),
-> platform VARCHAR(255),
-> platformId INT(11) unsigned,
-> price DECIMAL(4,2),
-> index(platformid),
-> FOREIGN KEY (platformId) REFERENCES Platforms(platformId)
-> );
Query OK, 0 rows affected (0.24 sec)
root@localhost(test2) show tables;
-----------------
| Tables_in_test2 |
-----------------
| game |
| platforms |
-----------------
2 rows in set (0.00 sec)
root@localhost(test2) show create table game \G
*************************** 1. row ***************************
Table: game
Create Table: CREATE TABLE `game` (
`gameId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`publisher` varchar(255) DEFAULT NULL,
`platform` varchar(255) DEFAULT NULL,
`platformId` int(11) unsigned DEFAULT NULL,
`price` decimal(4,2) DEFAULT NULL,
PRIMARY KEY (`gameId`),
KEY `platformId` (`platformId`),
CONSTRAINT `game_ibfk_1` FOREIGN KEY (`platformId`) REFERENCES `platforms` (`platformId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)