![](/img/trans.png)
[英]Trying to add a table with SQL, getting “invalid identifier” error with foreign key
[英]New to SQL - Trying to add a foreign key to my table but getting error (key column ' ' does not exist)
我對 SQL 很陌生,我正在嘗試創建一個食譜數據庫。 目前,我設置了 2 個表,我正在嘗試使用外鍵加入,但在嘗試添加鍵時我不斷收到錯誤消息。
第一張表:
CREATE TABLE recipe(
recipeID INT UNSIGNED NOT NULL AUTO_INCREMENT,
recipeName VARCHAR(255),
created TIMESTAMP,
updated TIMESTAMP,
PRIMARY KEY (recipeID)
);
第二張表:
CREATE TABLE instructions (
instructionsID INT UNSIGNED NOT NULL AUTO_INCREMENT,
specification LONGTEXT,
listOrder INT,
PRIMARY KEY (instructionsID),
FOREIGN KEY (recipeID) REFERENCES recipe(recipeID)
);
錯誤:錯誤 1072 (42000):表中不存在鍵列“recipeID”
配方表信息:
場地 | 類型 | Null | 鑰匙 | 默認 | 額外的 |
---|---|---|---|---|---|
食譜ID | int(10) 無符號 | 不 | PRI | NULL | 自動遞增 |
食譜名稱 | varchar(255) | 是的 | NULL | ||
創建 | 時間戳 | 不 | current_timestamp() | 更新 current_timestamp() | |
更新 | 時間戳 | 不 | 0000-00-00 00:00:00 |
說明表信息:
場地 | 類型 | Null | 鑰匙 | 默認 | 額外的 |
---|---|---|---|---|---|
指令ID | int(10) 無符號 | 不 | PRI | NULL | 自動遞增 |
規格 | 長文本 | 是的 | NULL | ||
列表順序 | 整數(11) | 是的 | NULL |
FOREIGN KEY (recipeID) REFERENCES recipe(recipeID)
在這里,您在說明表中將 recipeID 從 recipe 引用到 recipeID。 您在說明表中沒有recipeID。我認為您需要先創建recipeID 列,然后在recipe 表中使用recipeID 引用它
CREATE TABLE instructions (
instructionsID INT UNSIGNED NOT NULL AUTO_INCREMENT,
specification LONGTEXT,
listOrder INT,
recipeID INT UNSIGNED,
PRIMARY KEY (instructionsID),
FOREIGN KEY (recipeID) REFERENCES recipe(recipeID)
);
我猜這會起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.