MySql - Update for null column unique

2

Good afternoon guys.

I would like to know if it is possible to make a UPDATE to NULL where the field only allows UNIQUE values.

I tried this way:

update FUNCIONARIO set codigocartao = NULL where situacao = 'DESATIVADO'

But I got the duplicate registry error.

The scenario I need is: the encoding field must have a unique record, but it can also be null. the scenario I have today is: every time I need to set the field to NULL , I end up having to delete the table official.

Am I doing something wrong or does not this possibility exist in MySQL ? Anyone have any ideas?

    
asked by anonymous 03.06.2015 / 22:53

1 answer

1

Yes, you can do this. see MySQL documentation (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.

Make sure you have not defined the column as NOT NULL

    
03.06.2015 / 23:03