创建外键时MySQL Error 1064

时间:2018-03-10 17:38:45

标签: mysql mysql-workbench

我是使用MySQL WorkBench

创建的
ALTER TABLE `android_marketplace`.`ban_utilizator` 
DROP INDEX  ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC);
ALTER TABLE `android_marketplace`.`ban_utilizator` 
ADD CONSTRAINT `ban_utilizator_id_utilizator`
 FOREIGN KEY (`id_utilizator`)
 REFERENCES `android_marketplace`.`utilizatori` (`id_utilizator`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION;

我得到了这个:

Operation failed: There was an error while applying the SQL script to the 
database.
Executing:
ALTER TABLE `android_marketplace`.`ban_utilizator` 
DROP INDEX  ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC);
ALTER TABLE `android_marketplace`.`ban_utilizator` 
ADD CONSTRAINT `ban_utilizator_id_utilizator`
 FOREIGN KEY (`id_utilizator`)
 REFERENCES `android_marketplace`.`utilizatori` (`id_utilizator`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION;

ERROR 1064: You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use near '
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC)' at line 2
SQL Statement:
ALTER TABLE `android_marketplace`.`ban_utilizator` 
DROP INDEX  ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC)

1 个答案:

答案 0 :(得分:1)

错误与外键无关。

您必须为要删除的索引命名。

语法错误消息包括问题的上下文。如果语法错误说:

  

检查...以便在'附近使用正确的语法'   添加索引ban_utilizator_id_utilizator_idxid_utilizator ASC)'

这意味着语法期望在您提供ADD INDEX的位置找到其他内容。

在这种情况下,它期望您试图在ADD INDEX之前的子句中删除的索引的名称。