Against my will, I am told to use the following method to generate a new pk value.
INSERT
INTO
SERVER_MONITERING(SM_SEQ, CPU_USE, MEMORY_BASE, MEMORY_USE, DISK_BASE, DISK_USE, SERVER_IP, SERVER_NAME, OBSTACLE_YN, CHECK_TIME)
VALUES((SELECT MAX(SM_SEQ)+1 FROM SERVER_MONITERING), #cpuStat.combinedUsed#, #memStat.total#, #memStat.used#, #diskStat.size#, #diskStat.used#, #serverIP#, 'KW', 'N', TO_CHAR(SYSDATE, 'YYYYMMDDHH24MI'))
I told him it still looks like there could be duplicate SM_SEQ
values.
However, I don't know for sure whether this approach will eliminate duplication or not.
- Will the approach above eliminate the chance of duplicate values ?
- Is this a good practice ?
- Will a sequence.nextval eliminate the chance of duplicate values ?
- What is the best practice ?
Edit
Sorry, I forgot to mention.
There are multiple devs and their own code that inserts into this table and they all use select max()+1
. I am a "guest" dev at this company. So I have little influence on their decision. If I switch to using a sequence only by myself, other devs will still select max()+1
and there will be collision between the two approaches.
Probably, I would have to tell all of them why even nesting a select max()+1
won't solve their problem, if it truly would not.