Exception:
Violation of UNIQUE KEY constraint 'UQ__user_dat__AB6E61641273C1CD'. Cannot insert duplicate key in object 'dbo.user_data'.
The statement has been terminated.
AB6E61641273C1CD = email which is unique and NULL
My SQL statement does NOT insert a value for email. So i assume it will have the default as null (although i never wrote a default(null) statement when creating table).
What is the problem? The code worked in sqlite perfectly.
If you want a Unique Key that ignores Nulls it isn’t possible in SQL Server. However this article describes how you can get that behavior
via
Additionally AlexKuznestov’s answer notes that in SQL 2008 you can provide a filtered index.
The best of these is an indexed view but like a trigger its existence is not always apparent. The computed column is interesting but you would need to be able to guarantee that the actual data never clashes with the column you’re “casing” on.