The one-step answer to the first question is to use something like:
update TBL set CLM = CLM + 1 where key = 'KEY'
This is a very easy way to do this.
As in the second question, you do not need to resort to special SQL gymnastics, such as a DBMS (for example, UPSERT ), to get the desired result. There is a standard method for updating or inserting that does not require a specific DBMS.
try: insert into TBL (key,val) values ('xyz',0) catch: do nothing update TBL set val = val + 1 where key = 'xyz'
That is, you are first trying to make a creation. If it already exists, ignore the error. Otherwise, you will create it with a value of 0.
Then perform an update that will work correctly or not:
- the string originally existed.
- someone updated it between your insert and update.
This is not one instruction, and, nevertheless, it is amazing how we have been doing this successfully for a long time.
paxdiablo Jun 10 '09 at 2:21 2009-06-10 02:21
source share