Fixes to DB triggers, and add indexes

Add missing intermediate table deletion to delete trigger.
Add missing WHERE clause to insert in trigger.
Add indexes.
Yes, it's been added there in this commit.