title: How to add value if exists in another table
body: <p>I have table_a which has a foreign key column referencing table_b.  I'd like to fill the fkey column with values from column_a if the value exists in table_b's pkid column but null if it doesn't exist.</p>
  
  <p>Without looking for whether it exists, I was using the following query:</p>
  
  <pre><code>UPDATE table_a SET fkey = column_a;
  </code></pre>
  
  <p>That fails sometimes due to the foreign key constraint if column_a's value doesn't exist in table_b so I think I need to use some combination of CASE and EXISTS but I'm struggling with the syntax.</p>
  
  <p>The constraint looks like:</p>
  
  <pre><code>ALTER TABLE table_a ADD CONSTRAINT
      constraint_fkey FOREIGN KEY (fkey) REFERENCES table_b(pkid);
  </code></pre>