如何在MySQL中建立表之间的关系

时间:2018-11-03 08:07:11

标签: mysql go foreign-keys mysql-workbench go-gorm

我想在MySQL中的表之间建立某种关系。我使用GORM ORM进行操作。我有3个具有此名称的表:singertbl,covertbl,singerscovertbl。 我的表的ID为autoincrement值。 在singertbl和Covertbl中,我有以下字段:ID和{some another field}。 在singerscovertbl中,我有以下字段:ID,singer_id,cover_id。 {ID.singertbl-> singer_id.singerscovertbl}与 {ID.covertbl-> cover_id.singerscovertbl}当我想通过以下SQL查询将此关系作为MySQL工作台中的外键应用时:

    ALTER TABLE `mskm`.`albumscovers` 
ADD INDEX `ID_idx` (`album_id` ASC) VISIBLE,
ADD INDEX `ID_idx1` (`cover_id` ASC) VISIBLE;
;
ALTER TABLE `mskm`.`albumscovers` 
ADD CONSTRAINT `ID`
  FOREIGN KEY (`album_id`)
  REFERENCES `mskm`.`albumstbls` (`ID`)
  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 `mskm`.`albumscovers` 
ADD INDEX `ID_idx` (`album_id` ASC) VISIBLE,
ADD INDEX `ID_idx1` (`cover_id` ASC) VISIBLE;
;
ALTER TABLE `mskm`.`albumscovers` 
ADD CONSTRAINT `ID`
  FOREIGN KEY (`album_id`)
  REFERENCES `mskm`.`albumstbls` (`ID`)
  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 `ID_idx1` (`cover_id` ASC) VISIBLE' at line 2
SQL Statement:
ALTER TABLE `mskm`.`albumscovers` 
ADD INDEX `ID_idx` (`album_id` ASC) VISIBLE,
ADD INDEX `ID_idx1` (`cover_id` ASC) VISIBLE

如何在此表之间建立这种关系?!

0 个答案:

没有答案