views:

203

answers:

6

Whenever we have to update the database; we delete the values from the table first and then add the latest values. This ensures that everything is updated correctly.

This adds little bit overhead to the system but we haven't faced any performance issues because of this.

Is this always the best thing to do?

+3  A: 

No.

Use the UPDATE statement.

Additionally, if you worried about integrity, scope it within a transaction:

BEGIN TRAN T1

-- This update is part of T1
UPDATE Table1 SET Col1='New Value' WHERE Col2 = @Id;

-- Time to commit your changes. 
-- If for any reason something fails, 
-- everything gets rolled back
COMMIT TRAN T1
FlySwat
This doesn't explain anything because deleting and inserting would be exposed to even worse transaction integrity issues.
le dorfier
+2  A: 

No.

If the table is indexed (most are) but you don't have a regular index maintenance task running to rebuild (defragment) the index, updating will be preferable. Deleting and recreating rows will lead to fragmentation over time.

Mitch Wheat
wtf was this downvoted?!?
Mitch Wheat
I didn't downvote this, but can you explain it? The only database product I'm familiar with that's so sloppy about reusing and dynamically defragmenting its tables is MySQL with the standard table types. Even Access does much better than that.
le dorfier
when you insert into a table with a clustered index, and the pages are full, the point of insertion requires that pages are split to make way for the new item. This leads to fragmentation of the index.
Mitch Wheat
@doofledorfer: I don't think Access does any better. You have to periodically compact an access database. Compact reclaims space left by deletes, so its a similiar situation.
Mitch Wheat
But if you take an entry out, it removes it from the index and the space is available to be reused. And if it's different pages, it just gets absorbed in the natural activity of moving pointers among pages. (Usually they don't fill up before they split. They like to have some room left in the node.)
le dorfier
I'm familiar with Access' "Compact" activity, but that's to make it smaller more than to make index adjustments because things are in the wrong places.
le dorfier
I have the nasty job of supporting an Access-based application; the owner uses the "Compact Database" advice as a placebo for his users; which is natural, since he's an MD. :)
le dorfier
I don't recall any such operation being required or recommended for SQL Server or Sybase or Informix; and I doubt Oracle could require such a thing.
le dorfier
Looks like someone retracted their downvote. :)
le dorfier
I down-voted because in Oracle regular index rebuilds are not required. Also, since I can't think of any good reason to perform a delete-insert instead of an update it seems arbitrary to just identify one reason that might at best apply only to a subset of database systems
David Aldridge
I downvoted it because worrying about index fragmentation when the OP doesn't even know about UPDATE is like discussing fuel efficiency concerns with someone who doesn't know how to steer a car. It's the least of the OP's problems.
Schwern
A: 

By "database" I presume you mean a SQL database. The danger of doing a delete then insert is, unless you're careful with your transactions, it is non-atomic. In the time between deleting the row and inserting the new one someone might have slipped a similar row in causing the insert to fail on some constraint. It's also a waste of effort because there's update, as J. Holland points out. It'll all do it in one shot. Putting it alone in its own transaction is probably unnecessary as updates are atomic.

If you're doing the delete-then-insert because you're not sure if the row already exists, some databases have a replace statement which will insert if there's no row or update if there is.

Schwern
+1  A: 

How is it that everything is not updated correctly otherwise?

le dorfier
A: 

As everyone else said, use UPDATE.

One extra reason: if you have CASCADE DELETE on any referential integrity constraints, DELETE then INSERT loses the subordinate data. If you don't have CASCADE DELETE, then you may be unable to DELETE the record because it is still referenced by other tables.

Jonathan Leffler
A: 

A delete-insert is a higher cost operation than an update.

If the reason is not obvious, it is because you have to store undo/redo information for the complete row being removed, including the modification of all index entries and checks for referential integrity violations caused by the deletion, followed by the insert of a new row with all of the undo/redo, index entry, integrity checks etc.. the new row might also be in a different block to the deletion so now you are doubling (at least) the physical i/o requirements.

Also consider the impact on triggers and logging. And you have to grant insert and delete privileges when you could just be granting update privileges.

I'm sure that there is a regular programming paradigm, such as "When I want to assign a new value to a variable, I always set it to zero first ..." but i fear that it wouldn't convey the full horror of the situation.

David Aldridge