title: How to create a Constraint for 2 columns mysql
body: <p>I have have created a database with 3 tables:<br>
  Products<br>
  Categories<br>
  Subcategories</p>
  
  <p>In my products table I have a category and a subcategory column:  </p>
  
  <pre><code>| product_name | category | subcategory|  
  ----------------------------------------    
  | varchar      | int      | int        |
  </code></pre>
  
  <p>As of right now the category column references the categories table id and the subcategory column references the subcategories id.</p>
  
  <p>The categories column has an id and a category name column:</p>
  
  <pre><code>| id | category_name |
  ----------------------
  |int | varchar       |
  </code></pre>
  
  <p>The subcategories column has an id, sub category and a parent column which is a  foreign key that references the categories(id) column.</p>
  
  <pre><code>| id | subcategory_name | parent |
  ---------------------------------
  |int | varchar          | int    |
  </code></pre>
  
  <p>I wanted to know if there is a way to add a constraint to the products's 'subcategories' column so that one would only be able to add a value if it was a legitimate child of the parent category. </p>
  
  <p>Forgive me if this question has been answered already. I do not know the proper name for what I am trying to do.</p>