title: Postgresql include "optional", nullable foreign key?
body: <p>So I have a probably stupid problem and I don't even know if I'm trying to solve it the right way, but here it is.</p>
  
  <p>I have a department with several employees, an employee can be assigned to a department but doesn't have to be. I thought of using the department no as foreign key in employee, but what about employees who are not part of a department?
  Can I just insert null in the fk column? But I'm wondering if that even works because can't I insert only values that exist in the referenced column?</p>
  
  <p>Here's my relevant code</p>
  
  <pre><code>CREATE TABLE department(depno int unique not null,
                          primary key(depno));
  
  CREATE TABLE employee(ID int unique not null, name not null,
                        depno int,
                        primary key(ID))
                        foreign key(depno)
                        references department(depno)
  </code></pre>
  
  <p>I hope this question hasn't been answered a hundred times already and I just haven't found my answer while searching for it for the last two hours. But I just can't seem to find anything about whether this should work or not.</p>
  
  <p>Any tips or solutions would be greatly appreciated!
  Thank you so much already!</p>