On Thu, 2002-03-28 at 03:14, David Cantrell wrote: > On Wed, Mar 27, 2002 at 06:29:10PM -0500, Matthew Brooks wrote: > > MessageChecking for the existence of a particular record in a database before >performing an UPDATE/DELETE on it is just good programming practice. An UPDATE/DELETE >by definition, and in comparison to an INSERT, assumes that the record already exists. > > It is acceptable to check the return value though to see if the UPDATE/DELETE > succeeded. If n rows are updated or deleted, DBI will return you an n. > > Checking before doing something is not always the recommended way of working. > Do you check for a file's readability before open()ing it? No, you > open || report failure. I treat database updates the same way.
Just so. In fact, I would say that it's BETTER practice to NOT check. Why? Because it's far more efficient, and it doesn't require a transaction in order to be atomic (which makes it even MORE efficient if you care about atomicity). Certainly, executing two independant SQL statements to check for the existance of a record and then to update or delete it would be error prone if you did not use a transaction. In the end, it's going to depend on your application, but certainly checking first should not be considered REQUIRED in the general case.
