Avoid Sql Conflicts When Inserting data At Same Time Let suppose I have a query that runs multiple times because it is in a while loop which inserts data into a table. 

INSERT INTO &lt;table&gt;( psuiKey, group_id) VALUES ('54ds8a784daEd','1');


The problem is when two users from different computers add data at same time. So what is happening is user 1 adds data to the table to looks like this:

psuiKey       group_id 
54ds8a784daEd  1
54ds8a785daEd  1


and at the same time user 2 adds data where group_id is 2, but the data looks like this:

psuiKey       group_id 
54ds8a784daEd  1
54ds8a785daEd  1
54ds8a786daEd  1
54ds8a787daEd  1


instead of:  

psuiKey       group_id 
54ds8a784daEd  1
54ds8a785daEd  1
54ds8a786daEd  2
54ds8a787daEd  2


So in short if two users add the same data at same time so group_id is not changing when I find max group_id using: 

SELECT MAX(group_id) + 1


So SQL can't able to update it when two users add data at the same time. 

Note I cant use group_id as primary key and I also use datetime.

So any help would be helpful!
There are many alternatives you could attempt, such as enforcing an exclusive table lock on the SELECT query (thus delaying one entry until after the other). 

My personal favorite would be to enforce staging - production tables just like many of the testing apps I manage. 

This way, it doesn’t matter the timing or order. Remember the group_ID column is a database column, so it’s best not to get lost in the details over a value whose only purpose is to enforce differentiating groups