Safe table replacement in SQL