MySQLWorkbench 语法错误

MySQLWorkbench Syntax Error

我试图在 MySQLWorkbench 中执行以下 SQL 命令,但出现错误。

命令:

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

生成

MYSQL 说这是一个语法问题,正如我所见,您的代码中缺少一些东西。

请查看此 link 并在 mysql 中了解有关约束的更多信息。

希望对您有所帮助。

编辑:

好的,所以只是为了强制执行 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) 

两个列表中都必须有一列(或多列)。不能为空。

例如:

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

列的数据类型完全匹配。存储在外键列中的值必须与引用的 table 中一行中的值匹配。 (在此示例中,profile_id 中的所有值必须与 profiles table 中的 id 列的值匹配。