2015-07-02 31 views
1

我試圖在MySQLWorkbench中執行下面的SQL命令,但它給我一個錯誤。MySQLWorkbench語法錯誤

命令:

ALTER TABLE `ABC`.`GroupMembers` 
ADD CONSTRAINT `FK_PROFILES` 
    FOREIGN KEY() 
    REFERENCES `ABC`.`profiles`() 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION; 

錯誤:

Operation failed: There was an error while applying the SQL script to the database. 
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') 
    REFERENCES `ABC`.`profiles`() 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION' at line 3 
SQL Statement: 
ALTER TABLE `ABC`.`GroupMembers` 
ADD CONSTRAINT `FK_PROFILES` 
    FOREIGN KEY() 
    REFERENCES `ABC`.`profiles`() 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION 

不知道這是怎麼回事。這個腳本是由MySQLWorkbench生成的

回答

2

必須有一列(或列)在這兩個列表。那些不能是空的。

例如:

FOREIGN KEY (profile_id) 
    --   ^^^^^^^^^^ 
    REFERENCES `ABC`.`profiles` (id) 
    --       ^^ 

列多匹配exhactly的數據類型。存儲在外鍵列中的值必須與被引用表中的一行中的值匹配。 (在這個例子中,在profile_id所有值必須在id列的值在profiles表相匹配。

1

MYSQL是說它的一個語法問題,正如我所看到的,在你的代碼中缺少一些東西。

請檢查此link並瞭解有關約束的mysql中sintaxis的更多信息。

希望它有幫助。

編輯:

好了,只是執行spencer7593的答案(這應該被標記爲答案,如果它解決您的問題):

... 
/profile_id would be the name you will set to the foreign key 
FOREIGN KEY (profile_id) 
... 
/The references should be of the same value. 
/`table_name`.`column_name` is the referenced column 
/id is the column on the table which will hold foreign key 
REFERENCES `ABC`.`profiles` (id) 
+0

包括在你的答案中的錯誤和解決方案吧。 – sqluser

+0

爲非常抱歉,試圖鼓勵他了解問題而不是爲他修復它。不會再發生,謝謝你的評論。 –