patternsqlMinor
Is it possible to have unique + null
Viewed 0 times
uniquepossiblehavenull
Problem
I have a need to have a database table that has a column that is either null, or unique values. I know I can control that in code, but is there a way to do that with an index as a backup? Or is that just really bad DB design? :) For example, I don't want to require an email address, but if they do enter one, I don't want to allow duplicates. Easy enough to check with my code, but I would like to configure Mysql to enforce it. Maybe a trigger returning false if the rules are violated?
Solution
For all engines, a UNIQUE index permits multiple NULL values for
columns that can contain NULL.
http://dev.mysql.com/doc/refman/5.7/en/create-index.html
It is easily testable, just try to insert multiple NULL values in dummy table.
columns that can contain NULL.
http://dev.mysql.com/doc/refman/5.7/en/create-index.html
It is easily testable, just try to insert multiple NULL values in dummy table.
Context
StackExchange Database Administrators Q#132518, answer score: 4
Revisions (0)
No revisions yet.