title: How to create relationship between two tables mysql
body: <p>I have two tables:</p>
  
  <p>topics table:</p>
  
  <pre><code>CREATE TABLE `topics` (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `topicId` varchar(45) NOT NULL,
    `title` text NOT NULL,
    `details` text,
    `dayPosted` varchar(45) DEFAULT NULL,
    `username` varchar(45) DEFAULT NULL,
    `userImage` varchar(45) DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>comments table:</p>
  
  <pre><code>CREATE TABLE `comments` (
    `commentId` int(11) NOT NULL,
    `topicId` varchar(45) NOT NULL,
    `comments` text,
    `commentDate` varchar(45) DEFAULT NULL,
    PRIMARY KEY (`commentId`),
    KEY `topicId_idx` (`topicId`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p><code>topicId</code> column is common in both the tables and I want to create relationship between both the tables. The problem is topicId is not a primary key(I can make it unique though)</p>
  
  <p>Or do I create a third table like <code>topics_comments</code> and have both:</p>
  
  <ol>
  <li>topicId as foreign key relationship to topicId in topics with
  cascade on update and delete</li>
  <li>commentId as foreign key relationship to commentId in comments with
  cascade on update and delete</li>
  </ol>
  
  <p>Actual scenario is I have blog posting site where users will comment on that blog where each blog topic has a <code>topicId</code> and each user comment has <code>commentId</code> and now I have create tables based on condition:</p>
  
  <ol>
  <li>Each blog topic can have multiple comments</li>
  <li>when blog topic is deleted, all the comments on that blog topic must
  be deleted</li>
  <li>Users who posted comments can later delete them or edit them</li>
  </ol>
  
  <p>I am using mysql with <strong>spring boot</strong> and <strong>spring jpa</strong></p>
  
  <p>Entity class:</p>
  
  <pre><code>@Entity
  @Table(name = "topics")
  public class TopicBean implements Serializable {
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      @Column(name = "topicId")
      private String topicId;
      @Column(name = "title")
      private String title;
      @Column(name = "details")
      private String details;
      @Column(name = "username")
      private String username;
      @Column(name = "userImage")
      private String userImage;
      @Column(name = "dayPosted")
      private String dayPosted;
  
  //Getters and setters
  }
  </code></pre>