MySQL Create a table with index errors

Ok, so I create tables in MySQL with indexes and foreign keys. I use MySQL Workbench to create tables, and then redirect the SQL create script (better in the visual database environment than just writing the SQL code manually).

The problem in many cases, when I import a sql script into mysql, I get a classic eror:

#1005 - Can't create table 'db.tablename' (errno: 121) 

I managed to deal with the problem every time, usually associated with the index / foreign key, but now I'm starting to get annoyed with every fix. I don’t quite understand what the problem is (especially when the MySQL product creates SQL code for its own database). Below is the code that usually causes the problem.

CREATE  TABLE IF NOT EXISTS `db`.`groupMembers` (
  `groupMembersID` INT NOT NULL AUTO_INCREMENT ,
  `groupID` INT NOT NULL ,
  `userID` INT NULL ,
  PRIMARY KEY (`groupMembersID`) ,
  INDEX `group` (`groupID` ASC) ,
  INDEX `user` (`userID` ASC) ,
  CONSTRAINT `group`
    FOREIGN KEY (`groupID` )
    REFERENCES `db`.`groups` (`groupsID` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `user`
    FOREIGN KEY (`userID` )
    REFERENCES `db`.`users` (`usersID` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

INDEX - , . , .

+3
1

"errno 121

, , FK. , FK_groupMembers_group FK_groupMembers_user.

+4

Source: https://habr.com/ru/post/1715638/


All Articles