简体   繁体   English

我正在创建两个表,在其中一个表中,我试图分配一个外键,但它给了我一个错误

[英]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:有几点需要注意:

  1. The 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.按原样使用时出现错误。
  2. 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.当您执行FOREIGN KEY (platformId)时,此部分引用当前表中的列,然后此列引用的另一个表是: REFERENCES Platforms(platformId)部分。
  3. The foreign key's (column) reference in the current table must have an index.当前表中的外键(column)引用必须有一个索引。
  4. 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:所以,了解这些事情,在下面的例子中:

  1. I've replaced LONG with INT(11) unsigned in both tables.我在两个表中都将LONG替换为INT(11) unsigned
  2. Added a column for platformId in the game tablegame表中为platformId添加了一列
  3. Adds an index to the 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.

相关问题 在“关系视图”中,当我尝试创建外键时,为什么会给我一个错误? - In Relation View, why is it giving me an error, when I am trying to create a foreign key? 在 laravel 中创建外键时出现错误 - I had an error when i am creating a foreign key in laravel 我正在尝试创建一个只有两个外键的表 - I am trying to create a table with only two foreign key 我正在尝试使用外键约束连接两个表 - I'm trying to connect two tables with Foreign Key constraints 我有两个表,它们有一对多的关系,我试图从两个表中通过 id 检索数据 - i have two tables , which have one to many relationship , I am trying to retrieve data by id from both tables 我正在尝试比较两个不同数据库表中的项目 - I am trying to compare items in two different DB tables 我正在尝试查询 laravel 数据库中的两个表 - I am trying to query two tables in my laravel database 我试图在一个查询中从两个不同的表中选择数据 - I am trying to Select data from two different tables in one query Mysql错误:1222从两个现有表创建新表时 - Mysql error: 1222 when I am creating a new table from two existing tables 我正在尝试加入 2 个表并希望获取该行(如果它存在)但它向我显示此错误 SQLSTATE[42000]:语法错误或访问冲突:1064 - I am trying to join 2 tables and wanna fetch the row if it exists but it show me this error SQLSTATE[42000]: Syntax error or access violation: 1064
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM