title: Query to find foreign keys
body: <p>I have a database where I need to drop some foreign keys, but I don't know beforehand whether the foreign keys still exist.</p>
  
  <p>I've found some stored procedures (<a href="http://forums.mysql.com/read.php?97,218825,247526" rel="noreferrer">http://forums.mysql.com/read.php?97,218825,247526</a>) that does the trick, but I don't want to create a stored procedure for this.</p>
  
  <p>I've tried to use the query inside the stored procedure, but I get an error using "IF EXISTS (SELECT NULL FROM etc.. etc...</p>
  
  <p>Can I only use <code>IF EXISTS</code> in stored procedures?</p>
  
  <hr>
  
  <p>right now, the only thing I can run is</p>
  
  <pre><code>SELECT * FROM information_schema.TABLE_CONSTRAINTS 
  WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' 
  AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
  AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';
  </code></pre>
  
  <p>and I've tried this too</p>
  
  <pre><code>IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
  (...) do something (...)
  END IF; 
  </code></pre>
  
  <p>but I get a <code>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF' at line 1</code></p>
  
  <p>I've looked for examples in forums with simple queries and I can't make sense of why this isn't working.</p>
  
  <p>NOTE: Edit to correct broken link</p>