Unique constraint that allows empty values in MySQL

constraints, database-design, mysql

Solution

Yes, you can do this. See the MySQL reference (version 5.5).

A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.

Problem

I have a field that stores product codes. The codes are unique, but some products simply doesn't have a code. I can't invent codes because those are providers codes. Is this kind of constraint possible in MySQL? I'm a noob with stored procedures and triggers, so if the solution involves one of these, please be patient. Update: The column is NOT Null. That's why I was unable to do this.

Original source

Related problems