Relying on program to ensure uniqueness is unreliable, because under high concurrency conditions, the following 2 steps will overlap, resulting in repeated insertion:
Check whether there is already a record in the database
When establishing a unique constraint, the system will usually automatically create an index to ensure uniqueness while increasing query performance. (PostgreSQL Documentation)
Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint.
The author understands what the purpose of the database is. The database system is developed from the file system. If you follow the second option, you can directly store it as a file. Why use a database? The database is not just simple SQL. As a DBMS, it also provides many other functions, allowing you to concentrate on solving application problems while optimizing the underlying access of the system.
Relying on program to ensure uniqueness is unreliable, because under high concurrency conditions, the following 2 steps will overlap, resulting in repeated insertion:
Check whether there is already a record in the database
If not, insert record
It is better to choose the unique constraint:
Simplify application logic.
When establishing a unique constraint, the system will usually automatically create an index to ensure uniqueness while increasing query performance. (PostgreSQL Documentation)
The author understands what the purpose of the database is. The database system is developed from the file system. If you follow the second option, you can directly store it as a file. Why use a database? The database is not just simple SQL. As a DBMS, it also provides many other functions, allowing you to concentrate on solving application problems while optimizing the underlying access of the system.