Your question is not really clear, which is probably caused by your sample data, where the column contains both numbers and an empty string.
As I understand it, you want to replace certain strings (namely the empty string and '-1') with null. You want to do this very often, because data is added to the table all the time and you want to care about those "invalid" entries. You are worried about performance and wish you could somehow have the DBMS only look at new rows and thus don't read the whole table every time you update.
As you are showing an empty string in the column, I suppose the column's type is a string type (e.g. VARCHAR
). The first thing to consider is in my opinion hence, whether you want this column to be a string at all. If it were a number column (e.g. INT
), there would be no empty strings to start with. Also, -1 would always be just this same number, while a string can have leading or trailing space or special characters, you don't see.
Then, you seem to consider -1 undesired or invalid. So, why allow it to be entered? There are two ways to avoid such entries:
- A check constraint throwing an exception on inserts with invalid data.
- A trigger that changes the invalid value automatically to something valid.
With any of the two methods you won't have invalid data in the table anymore and there will just be no need to run repair updates.