[英]I am Creating two tables and in one of the tables I am trying to assign a foreign key but it is giving me an error
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我正在创建两个表,一个是平台表,一个是游戏表,我收到一条错误消息,说它在 sql 声明中找不到 PLATFORMID,但我不明白为什么
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)
);
The error is because a platformId
reference is not found in the game
table.该错误是因为在
game
表中找不到platformId
引用。
A few things to note:有几点需要注意:
LONG
data type is an alias for MEDIUMTEXT
, which surely would not have AUTO_INCREMENT
? LONG
数据类型是MEDIUMTEXT
的别名,它肯定不会有AUTO_INCREMENT
吗? I get an error with the use as-is.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.FOREIGN KEY (platformId)
时,此部分引用当前表中的列,然后此列引用的另一个表是: REFERENCES Platforms(platformId)
部分。(column)
reference in the current table must have an index.(column)
引用必须有一个索引。 So, knowing those things, in the example below:所以,了解这些事情,在下面的例子中:
LONG
with INT(11) unsigned
in both tables.LONG
替换为INT(11) unsigned
。platformId
in the game
tablegame
表中为platformId
添加了一列platformId
column in the game
table as well.game
表中的platformId
列。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.