[Solved] Sql update statement for unique field


If you update (or insert into table), and the new inserted/updated col1 value is already being used on some row, your query should fail with unique constraint violation error.

Make your application handle this error/exception properly and there’s really no need to check the existence of this value before the update/insert. The less queries your application makes the better it is for everyone…

However, I’d like to draw your attention to the following:
You already seem to have an ID column in your table. If it’s a Primary Key column, then it’s definitely unique too. Now consider, if you really need to have two unique key columns in the table. If your application logic is based on the unique values in the col1 column, you can actually drop the ID column and turn col1 into Primary Key instead 🙂

1

solved Sql update statement for unique field