title: Sqlite DELETE trigger with foreign key
body: <p>I've got a simple scheme:<a href="https://i.stack.imgur.com/96YdO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/96YdO.png" alt="scheme" /></a></p>
  <p>Basically, I have an incomerecord and incometype and since its a n-n relationship I've got an additional table to track which and how many types of income an incomerecord has.</p>
  <p>What I am trying to do is when a delete an <code>IncomeRecordWithTypeValue</code> record I want to check if there are any entities left with the IncomeRecord FOREIGN KEY, if not I want to delte the IncomeRecord.</p>
  <p>My current approach looks like this:</p>
  <pre><code>database.execSQL(
  &quot;CREATE TRIGGER IF NOT EXISTS Trigger_IncomeTypeIncomeRecordDelete &quot; +
  &quot;AFTER DELETE ON IncomeRecordWithTypeValue &quot; +
  &quot;BEGIN DELETE FROM IncomeRecord &quot; +
  &quot;WHERE ( SELECT * FROM IncomeRecordWithTypeValue WHERE IncomeRecordWithTypeValue.incomeRecordId = IncomeRecord.id &quot; +
  &quot;HAVING COUNT(*) = 0)&quot; +
  &quot;END&quot;);
  </code></pre>
  <p>However, I receive the following syntax error:</p>
  <pre><code>BETWEEN, IN, LIMIT, ORDER or semicolon expected, got 'END'
  </code></pre>
  <p>What am I doing wrong? My approach is not that bad right? My other approach was using inner join but I basically receive the same error</p>