I would like to know what behavior / differences between a Unique Index and Unique Constraints for Sql Server ?
Many should think that this does not make a difference in the database, but it does! And not only for the DBA / Administrator, but for developers too as it can influence how a code is written.
As a reference of what may be so different and what I'm interested in knowing below is an example of how it is in an Informix database, but in SQL Server how does it work?
UNIQUE Index
- Can be created / deleted ONLINE, with users using the table
This can weigh heavily on the maintenance of 24x7 systems. * The validity of oneness is done line by line. A simple example would be how execute aUPDATE tp01 SET cod = cod + 1;
in a table that field cod is sequential and with unique index, this update would give error on the 1st line since it would already double * Like all other databases, can be used as a reference for Foreign keys ...Unique Constraint
(or even primary key constraint)
- Can not be deleted / created with users accessing the table, even than just reading. In other words you need exclusive access in the table to perform maintenance. That sucks in systems 24x7. * Validation is done at the end of the statement block. case of
UPDATE tp01 SET cod = cod + 1;
, would work without problems because it will validate the uniqueness only when finalizing the update of all the lines involved. * It is possible to postpone the validation of constraint only at the end of the transaction. That is, at the time ofcommit
. This is possible when theset constraints [all|<constraint>] deffered;
command is used before the updates. So in the above update example, it will only validate the uniqueness when commit is sent to the bank. Note: This feature valid for all types of constraints (PK, FK, UK)
Source: Difference in use Index Unique and Unique Constraint in Informix?
Remembering, the above description is how IBM Informix works. I would like to know how it is in other banks and understand what limitations or additional features we can provide for developers.