title: Problems understanding FOREIGN KEY / CASCADE constraints
body: <p>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.</p>
  
  <p>Let's assume I've got the following tables (and an arbitrary number of other tables that may reference table <code>tags</code>):</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>So I create a further table to provide the many-to-many relation between <code>news</code> and <code>tags</code>:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>My requirements to the cascades:</p>
  
  <ul>
  <li>If I delete a news, all corresponding entries in <code>news_tags</code> should be removed as well.</li>
  <li>Same applies for table <code>x</code> that may be added later with <code>x_tags</code>-table.</li>
  <li>If I delete a tag, all corresponding entries in <code>news_tags</code> and in every further table <code>x_tags</code> should be removed as well.</li>
  </ul>
  
  <p>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.</p>
  
  <p>Any links to good tutorials, SQL-queries or JPA-examples appreciated! </p>