I have a MySQL MyISAM table with ISBNs. I want to create a UNIQUE index that won’t throw “duplicate” error if the value is empty or null.
Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you’re using MyISAM (or INNODB) storage engine, the short answer is no. Multiple empty strings won’t work for the unique index constraint, even though multiple nulls would. Your best bet if you want a SQL only solution, is to create a regular index on the ISBN field, and then use a stored procedure to act as an insertion proxy–one that checks for uniqueness if not null or empty.
If just nulls work for you, here is a basic create:
Just a note… Remember the ’13’ specified for int is only the number of digits it will show when returning in a query, not the size of the integer.