title: Add foreign key constraint if it is not existing
body: <p>I want to create a foreign key from 1 table, but only if it does not exist.</p>
  <p>Tables are created like that:</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE 
  IF NOT EXISTS PEs (
      id INT(20) AUTO_INCREMENT PRIMARY KEY, 
      Name varchar(20), 
      Message varchar(30), 
      CoordsX double(9,6) SIGNED,
      CoordsY double(9,6) SIGNED,
      CoordsZ double(9,6) SIGNED,
      Status smallint(1) DEFAULT 1,
      world varchar(20)
  ) ENGINE = InnoDB;
  </code></pre>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE 
  IF NOT EXISTS`rh_pe`.`attributes` (
      `toid` INT(20) NOT NULL, 
      `Kommentar` VARCHAR(60) NOT NULL, 
      `Aktion` varchar(10) NOT NULL, 
      `Person1` INT NOT NULL,
      `Person2` INT
  ) ENGINE = InnoDB;
  </code></pre>
  <p>The Foreign key should be like so:</p>
  <pre class="lang-sql prettyprint-override"><code>ALTER TABLE `attributes` 
  ADD CONSTRAINT `Const`
  FOREIGN KEY (`toid`) REFERENCES `pes`(`id`)
  ON DELETE RESTRICT
  ON UPDATE RESTRICT;
  </code></pre>
  <p>To create the foreign key, I tried the following two options:</p>
  <pre class="lang-sql prettyprint-override"><code>IF NOT EXISTS(
      ALTER TABLE `attributes` 
      ADD CONSTRAINT `Const` 
      FOREIGN KEY (`toid`) REFERENCES `pes`(`id`) 
      ON DELETE RESTRICT 
      ON UPDATE RESTRICT
  );
  </code></pre>
  <p>and</p>
  <pre class="lang-sql prettyprint-override"><code>ALTER TABLE `attributes` 
  ADD  CONSTRAINT `Const` 
  FOREIGN KEY 
  IF NOT EXISTS (`toid`) REFERENCES `pes`(`id`) 
  ON DELETE RESTRICT 
  ON UPDATE RESTRICT
  </code></pre>
  <p>But none of them work.</p>
  <p>Any Ideas on how I could create the constraint only if it does not exist?</p>