I have 2 Tables, one known as “Image” Table, other known as “Image_Question” Table. Below is what the 2 tables could look like:
Image Table:
ImageId ImageFile
01 picture/cat.png
02 picture/cat_2.png
03 picture/dog.png
Image_Question Table:
ImageId SessionId QuestionId
01 ADS 3
02 FTG 7
03 JJK 1
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.
I tried the code below but it did not work:
ALTER TABLE Image_Question ADD CONSTRAINT FK_ImageId FOREIGN KEY (ImageId) REFERENCES Image (ImageId) ON DELETE CASCADE;
UPDATE:
SHOW IMAGE TABLE:
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
SHOW IMAGE_QUESTION TABLE:
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
The only requirement for a foreign key is that it be an index, as far as I know. Your key in
Image_Questionsshould beQuestionId, and it looks like you’ve changed it to that already per my suggestion in a comment. But that’s not the reason your CASCADE DELETE doesn’t work.(A side note: as far as data organization goes, it makes no sense to have the same primary key for two tables in 99.9% of cases (only real case is to hide sensitive information like credit card numbers). If two tables have the same primary key, they should be the same table.)
Since you posted your
CREATE TABLEstatements, the problem is now totally obvious – onlyInnoDBsupports foreign key constraints in MySQL.MyISAMdoes not (yet). It is strange and misleading that they let you create MyISAM tables with foreign key syntax without error and don’t support it.The way you have it set up, deleting a row from table Image will delete the corresponding row(s) in Image_Question. That makes sense.
Foreign Keys set up Parent->Child relationships. If you kill the parent, the child dies. But if you kill a child, that doesn’t kill the parent. The parent will continue to exist, and may have many other children.
Each of your images might have 50 questions. You don’t want to delete the parent image when any one of those child questions is deleted. But if you delete 1 image, all of its child questions become irrelevant and useless. You CAN cascade delete, but you could
SET NULLorNO ACTIONif you think the child data could still be useful in some way. SET NULL would render allorphanrows equal, whereas NO ACTION would keep their referent-less foreign keys and so you could still group them by parent even though the parent is gone.This query should result in a cascade once you’ve changed your tables to InnoDB:
That will delete one row from
Imageswhere id is 2 (sinceidis the primary key and only one row per value can exist), and ALSO all rows fromImage_Questionswhere theImageIdis 2 (millions of rows could exist).Also note that it’s very weird for you to have
ImageIdas the first column ofImage_Questionsand it should NOT be the primary key. There is very rarely a reason to have two tables with the exact same primary key. The primary key ofImage_Questionsshould be theQuestionId.Refer to this for info on foreign key support in MySQL:
http://dev.mysql.com/doc/refman/5.5/en/ansi-diff-foreign-keys.html
Here is a link to the ON DELETE options:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html