title: Foreign key references itself
body: <p>I want to create a table for categories where if the <code>parent_id</code> is 0, then that category has no parent. Otherwise, any category can be the parent of another category:</p>
  
  <pre><code>CREATE TABLE categories (
    cat_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    parent_id INTEGER UNSIGNED NOT NULL DEFAULT 0,
    title VARCHAR(100) NOT NULL,
    valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
  
    PRIMARY KEY(cat_id),
    FOREIGN KEY (parent_id)
      REFERENCES categories(cat_id)
  );
  </code></pre>
  
  <p>The problem upon trying to insert a new value is:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
    update a child row: a foreign key constraint fails
    (<code>test</code>.<code>categories</code>, CONSTRAINT <code>categories_ibfk_1</code> FOREIGN
    KEY (<code>parent_id</code>) REFERENCES <code>categories</code> (<code>cat_id</code>))</p>
  </blockquote>
  
  <p>I assume this means that a <code>parent_id</code> cannot be 0, because no <code>cat_id</code> with that value exists. How can I get around this issue or better design my table?</p>