【问题标题】:Add Foreign Key in SQL by My-SQL Workbench gives Error 1064通过 My-SQL Workbench 在 SQL 中添加外键给出错误 1064
【发布时间】:2021-02-01 01:42:45
【问题描述】:

我想将外键添加到名为“地址”的表中。

ALTER TABLE `students1`.`address` 
ADD CONSTRAINT `id_country`
FOREIGN KEY ()
REFERENCES `students1`.`country` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION;

这个请求是由 MySQLWorkbench 自动生成的。 但是得到这个错误

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 `students1`.`country` ()
ON DELETE NO ACTION
ON UPDATE NO ACT' at line 3

如何解决这个问题?

【问题讨论】:

  • 这只是无效的 SQL。你到底想做什么?
  • 我正在尝试将外键添加到另一个已经存在的表中。

标签: mysql sql foreign-keys mysql-workbench


【解决方案1】:

我不知道你的两个表是否有相同的列id_country。

但这种情况下的语法是:

ALTER TABLE `students1`.`address` 
    ADD CONSTRAINT 
    FOREIGN KEY (`id_country`)
    REFERENCES `students1`.`country`(`id_country`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 2018-04-14
    • 2015-04-03
    • 2021-03-05
    • 2021-08-04
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多