DELETE, INSERT vs UPDATE || INSERT
I have a table in SQL that will be updated daily. Which is better, to delete the ex开发者_高级运维isting table and re-insert old and new values, or update existing values if changed, and add new ones?
If most of the data changes, and there is no need for integrity checks, rollbacks and such, truncating your table would be most efficient. And then re-inserting everything of course.
The best solution depends on circumstances and requirements. However, generally I wouldn't recommend deleting the table altogether.
Definitely to update the existing values in the table.
You shouldn't be deleting a table frequently and recreating it just to alter some values.
You should use insert, update and delete to insert new values to the table, update any existing ones that have changed, and delete ones you don't need any more. If you really want to empty the table you could do a "delete from [tablename]" with no where clause and that will clear all the values from the table, but don't go deleting the whole table and recreating it.
Depends on what you need...a complete wipe/replace works just fine if all you need is a "live" body of data, active records only for example.
Now if you need any history, you would need to do the update/insert route, but any records that are inactive would need a flag stating such.
I would almost always opt for the update/insert method, as you don't lose any data, and the moment you don't have it is when someone needs it.
Why would you delete the old table and reinsert old values? Insert & Update. Some more specifics of what you're trying to accomplish might help provide a better answer.
精彩评论