MySQLWorkbench语法错误

时间:2015-07-02 23:26:05

标签: mysql mysql-workbench

我正在尝试在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

生成

2 个答案:

答案 0 :(得分:2)

两个列表中都必须有一列(或多列)。那些不可能是空的。

例如:

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

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

答案 1 :(得分: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) 
相关问题