SQL Database: Fix next id and free id discovery.