Suppose, I need to delete or update some id informations from my database, which would be the best way to do it? Should I first find out that if that id exists or not? Or should I update the data then check the result if that comes false that means that data doesn't exist.
method 1
let find_id = "Select id from MyGuests where id=2";
if(find_id.length === 0 || find_id.length === undefined) {
return not found //immediately exit
}
let result = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"
return result;
or should I use this one?
method 2
let result = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"
if (result === 0) {
return not found
}
return result;
Same question is for my delete query also? Which one is the optimal way?