(#836) Prevent unsafe SQL statement

Added unique key to be able to use the INSERT IGNORE without the additional SELECT query.

(part of #836)