Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:16:39+00:00 2026-06-11T21:16:39+00:00

I have 2 Tables, one known as Image Table, other known as Image_Question Table.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:16:40+00:00Added an answer on June 11, 2026 at 9:16 pm

    The only requirement for a foreign key is that it be an index, as far as I know. Your key in Image_Questions should be QuestionId, 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 TABLE statements, the problem is now totally obvious – only InnoDB supports foreign key constraints in MySQL. MyISAM does 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 NULL or NO ACTION if you think the child data could still be useful in some way. SET NULL would render all orphan rows 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:

    DELETE FROM `Images` WHERE `id` = 2;
    

    That will delete one row from Images where id is 2 (since id is the primary key and only one row per value can exist), and ALSO all rows from Image_Questions where the ImageId is 2 (millions of rows could exist).

    Also note that it’s very weird for you to have ImageId as the first column of Image_Questions and 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 of Image_Questions should be the QuestionId.

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables : one table it matchs and the other is teams.
I have 2 tables. How do I know IDs that are in one table
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables one called fs_note the other called dumy_fs_note I created after
I have two tables connected with one to many relationship. Parent Table is a
I have ten master tables and one Transaction table. In my transaction table (it
I have 2 tables, one called products and one images. The table images hold
I have two tables. One with information about properties. The other stores 3 images
Scenario I have 3 database tables. One is for Images and the other two
I have two tables. One has album information, and the other is actual copies

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.