question

Farhan Ali avatar image
Farhan Ali asked

what is the advantage of giving null value to unique constrain

what is the advantage of giving null value to unique constraint ?
t-sqlsql-serverunique-constraint
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
Not entirely sure of the context of your question, but let me give an example of where I have used this in the past. Assume a Salary history table that has 4 columns: EmployeeID int not null -- FK to Employee table Salary money not null DateFrom datetime not null -- this is the start date/time of the salary DateTo datetime null -- this is the end date/time of the salary You could use a value of `null` in the `DateTo` to signify that this salary has not ended yet, i.e. is the current salary. By enforcing a unique constraint on that field, only one salary history could be deemed as 'current'. Together with other constraints to disallow overlapping date ranges, this would ensure that DateTo be either unique, or null. This is an over-simplified and trivial example, but it shows how it can be used.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Usman Butt avatar image Usman Butt commented ·
+1 for a nice one :)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.