Additional indexes and missing primary keys

Tested with MySQL, SQLite and PostgreSQL.
Refs #2142, obsoletes #2191 
@Raydiation @karlitschek @DeepDiver1975 @roha4000 @PatrickHeller

Sorry for the messy history, I forgot to change branch.