Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete corresponding row using ON DELETE CASCADE
    primarykey
    data
    text
    <p>I have 2 Tables, one known as "Image" Table, other known as "Image_Question" Table. Below is what the 2 tables could look like:</p> <p>Image Table:</p> <pre><code>ImageId ImageFile 01 picture/cat.png 02 picture/cat_2.png 03 picture/dog.png </code></pre> <p>Image_Question Table:</p> <pre><code>ImageId SessionId QuestionId 01 ADS 3 02 FTG 7 03 JJK 1 </code></pre> <p>What my question is that in SQL how do I write it so that ImageId's from both tables are linked together so that lets say I delete row from Image Table where ImageId = 01, that the ImageId = 01 (corresponding row) from the Image_Question Table is also deleted. </p> <p>I tried the code below but it did not work:</p> <pre><code>ALTER TABLE Image_Question ADD CONSTRAINT FK_ImageId FOREIGN KEY (ImageId) REFERENCES Image (ImageId) ON DELETE CASCADE; </code></pre> <p>UPDATE:</p> <p>SHOW IMAGE TABLE:</p> <pre><code>CREATE TABLE `Image` ( `ImageId` int(10) NOT NULL AUTO_INCREMENT, `ImageFile` varchar(250) NOT NULL, PRIMARY KEY (`ImageId`) ) ENGINE=MyISAM AUTO_INCREMENT=399 DEFAULT CHARSET=utf8 </code></pre> <p>SHOW IMAGE_QUESTION TABLE:</p> <pre><code>CREATE TABLE `Image_Question` ( `ImageQuestionId` int(10) NOT NULL AUTO_INCREMENT, `ImageId` int(10) NOT NULL, `SessionId` varchar(10) NOT NULL, `QuestionId` int(5) NOT NULL, PRIMARY KEY (`ImageQuestionId`), KEY `QuestionId` (`QuestionId`), KEY `SessionId` (`SessionId`), KEY `fk_imagequestionid` (`ImageId`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload