When it comes to deleting or updating ID information from a database, what is the most effective approach? Should I first check if the ID exists before making any changes, or should I update the data and then verify the result to see if the data actually exists?
Option 1
let find_id = "Select id from MyGuests where id=2";
if(find_id.length === 0 || find_id.length === undefined) {
return not found //exit immediately
}
let result = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"
return result;
Alternatively, should I consider this method?
Option 2
let result = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"
if (result === 0) {
return not found
}
return result;
The same question arises when dealing with delete queries. Which approach is more optimal in this case?