I realised that we do not need to modify the above codes.

Perhads, we can create a validation code first to check the duplication before we record data into the database.

Any recommendation and suggestion?