Create sql and fix bugs