Error appears in sql when trying to add multiple foreign keys

前端 未结 1 1134
情歌与酒
情歌与酒 2020-12-22 11:17

I am trying to create an index and multiple foreign keys in mysql but when I Include the code below:

CREATE INDEX par_ind on Image_Question (SessionId,Questi         


        
相关标签:
1条回答
  • 2020-12-22 11:51

    here i have sorted out your problem and worked fine for me

    ALTER TABLE `question`     CHANGE `QuestionId` `QuestionId` INT(11) NOT NULL,    ADD PRIMARY KEY(`QuestionId`);
    

    first i have changed the QuestionId to primary key

    ALTER TABLE `image_question` ADD INDEX `questionId` (`QuestionId`);
    

    then added the index on QuestionId of image_question

    ALTER TABLE  `question` ADD CONSTRAINT `FK_question` FOREIGN KEY (`QuestionId`)
    REFERENCES `image_question` (`QuestionId`) ON DELETE NO ACTION ;
    

    and then first relationship for QuestionId works successfully

    ALTER TABLE `question`     CHANGE `SessionId` `SessionId` INT(11) NOT NULL;
    ALTER TABLE `image_question`     CHANGE `SessionId` `SessionId` INT(11) NOT NULL;
    

    then changed the data type of SessionId of both tables to int

    ALTER TABLE `image_question` ADD INDEX `NewIndex1` (`SessionId`);
    

    then added index on SessionId of image_question

    ALTER TABLE `image_question` ADD CONSTRAINT `FK_image_question` FOREIGN KEY (`SessionId`) REFERENCES `question` (`SessionId`) ON DELETE NO ACTION ;
    

    and here is your second relationship for SessionId hope it works fine for you also

    0 讨论(0)
提交回复
热议问题