Fix most of sql errors, still one error related to add constraint is outstanding