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 7058501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:07:13+00:00 2026-05-28T04:07:13+00:00

I need some help at understanding how foreign keys and cascades work. I understood

  • 0

I need some help at understanding how foreign keys and cascades work. I understood the theory but I’m having troubles to apply these to a real world example.

Let’s assume I’ve got the following tables (and an arbitrary number of other tables that may reference table tags):

CREATE TABLE tags (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) UNIQUE
) Engine=InnoDB;

CREATE TABLE news (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(63),
    content TEXT,
    INDEX (title)
) Engine=InnoDB;

So I create a further table to provide the many-to-many relation between news and tags:

CREATE TABLE news_tags (
    news_id INT UNSIGNED,
    tags_id INT UNSIGNED,
    FOREIGN KEY (news_id) REFERENCES news (id) ON DELETE ...,
    FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE ...
) Engine=InnoDB;

My requirements to the cascades:

  • If I delete a news, all corresponding entries in news_tags should be removed as well.
  • Same applies for table x that may be added later with x_tags-table.
  • If I delete a tag, all corresponding entries in news_tags and in every further table x_tags should be removed as well.

I’m afraid that I may have to revisit my table structure for this purpose, but that’s alright since I’m only trying to figure out how stuff works.

Any links to good tutorials, SQL-queries or JPA-examples appreciated!

  • 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-05-28T04:07:14+00:00Added an answer on May 28, 2026 at 4:07 am

    You seem to be proposing something like this, which sounds reasonable to me:

    CREATE TABLE tags 
    (
     id INTEGER NOT NULL, 
     name VARCHAR(20) NOT NULL, 
     UNIQUE (id), 
     UNIQUE (name)
    );
    
    CREATE TABLE news 
    (
     id INTEGER NOT NULL, 
     title VARCHAR(30) NOT NULL,
     content VARCHAR(200) NOT NULL,
     UNIQUE (id)
    );
    
    CREATE TABLE news_tags
    (
     news_id INTEGER NOT NULL, 
     tags_id INTEGER NOT NULL, 
     UNIQUE (tags_id, news_id), 
     FOREIGN KEY (news_id) 
        REFERENCES news (id)
        ON DELETE CASCADE
        ON UPDATE CASCADE, 
     FOREIGN KEY (tags_id) 
        REFERENCES tags (id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help understanding how stdext::hash_multimap's lower_bound, upper_bound and equal_range work (at least
Need some help understanding what the difference between these two redirects is: header( HTTP/1.1
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help understanding some C++ operator overload statements. The class is declared like
Need some help understanding why my concat() is failing and how to fix it.
I need some help understanding some of the points from Paul Graham’s What Made
I need some help understanding what's happening here. This code is from a models/log.py
I need some help understanding the correct way to mix variables with strings like
I'm new to ORM stuff and I need some help understanding something. Let's assume
I'm having a hard time wrapping my head around this and need some help

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.