DELETE, INSERT and UPDATE || INSERT

I have a table in SQL that will be updated daily. Is it better to drop the existing table and reinstall the old and new values, or update the existing values ​​if they have changed and add new ones?

+2


source to share


4 answers


If most of the data changes and there is no need for consistency checks, rollbacks, etc., table truncation will be most efficient. And then insert everything again.



The best solution depends on your circumstances and requirements. However, generally, I would not recommend dropping the table entirely.

+7


source


Definitely to update existing values ​​in the table.

You shouldn't drop the table often and recreate it just to change some values.



You should use insert, update and delete to insert new values ​​into the table, update any existing ones that have changed, and delete those you no longer need. If you really want to clear the table, you can do "delete from [tablename]" without the where clause and clear all values ​​from the table, but don't delete the entire table or recreate it.

+3


source


Depending on what you need ... a full clean / replace works just fine if you only want a "live" data body, active records only, for example.

Now, if you want history, you will need to follow the update / insert route, but any inactive records will require a flag pointing to such.

I would almost always choose the update / insert method, since you don't lose any data, and the moment you don't have it, that's when someone needs it.

+2


source


Why would you need to drop the old table and insert the old values? Insert and update. More information about what you are trying to accomplish may help provide a better answer.

0


source







All Articles