title: Delete parent record if child record does not exist CONSTRAINT PostgreSQL
body: <p>I am creating a small database to store information about hotel bookings and customers.</p>
  
  <p>I want to make it so that a room record in the room table can be deleted only if there are no bookings for a hotel room.</p>
  
  <p>The way I'm trying to solve the problem is by doing:</p>
  
  <pre><code>FOREIGN KEY (hid, room) REFERENCES room (hid, num) 
  ON DELETE CASCADE WHEN NOT EXISTS(hid AND room)
  </code></pre>
  
  <p>but it isn't working.</p>
  
  <p>Does anyone know what I'm doing wrong and how I can fix it?</p>
  
  <pre><code>CREATE TABLE booking( hid CHAR(4) NOT NULL, 
  cid CHAR(4) NOT NULL,    
  dstart DATE NOT NULL,    
  ndays DATE,    
  room INTEGER,     
  PRIMARY KEY(hid, cid, dstart), 
  FOREIGN KEY (cid) REFERENCES customer (cid)    
  ON UPDATE CASCADE    
  ON DELETE CASCADE,    
  FOREIGN KEY (hid, room) REFERENCES room (hid, num)    
  ON DELETE CASCADE WHEN NOT EXISTS(hid AND room)    
  );
  </code></pre>