2010-01-13 9 views
9

Diciamo che ho questo:Un enum in MySQL deve essere NOT NULL?

ALTER TABLE asdf ADD field ENUM('Y', 'N') DEFAULT 'N'; 

sta mettendo un NOT NULL alla fine necessario in quanto può essere solo Y e N?

EDT: in base ai commenti, se so che il software lo imposta sempre su "N" o "Y" ed è codificato in hard disk allora è OK lasciarlo spento o potrebbe ancora diventare potenzialmente nulla come.

risposta

-1

No, non è necessario, verrà impostato su "N" come probabilmente ci si aspetta.

Modifica: il commentatore mi ha fatto scattare e testare questo, è necessario aggiungere not null a meno che non si desideri che null sia un valore valido. Avendo default 'N' lo imposterà su N se lasci la colonna fuori dall'inserto SQL, ma se imposti il ​​valore su null nell'inserto o sull'aggiornamento, verrà inserita una riga con valore null, come potresti non volere.

+2

lettura http://dev.mysql.com/doc/refman/5.1/en/enum.html ("Se una colonna ENUM è dichiarata consenti NULL, il valore NULL è un valore legale per la colonna e il valore predefinito è NULL. ") Credo che si possa inserire correttamente NULL in questa colonna. –

11

MySQL consentirà il valore NULL se non si specifica NOT NULL nella definizione della colonna.

Ecco un test rapido:

mysql> create table test (id serial, field ENUM('Y','N') DEFAULT 'N'); 
Query OK, 0 rows affected (0.01 sec) 

mysql> INSERT INTO test (field) VALUES ('Y'); 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO test (field) VALUES ('N'); 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO test() VALUES(); 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO test (field) VALUES (NULL); 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO test (field) VALUES ('Invalid'); 
Query OK, 1 row affected, 1 warning (0.01 sec) 

mysql> show warnings; 
+---------+------+--------------------------------------------+ 
| Level | Code | Message         | 
+---------+------+--------------------------------------------+ 
| Warning | 1265 | Data truncated for column 'field' at row 1 | 
+---------+------+--------------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select * from test; 
+----+-------+ 
| id | field | 
+----+-------+ 
| 1 | Y  | 
| 2 | N  | 
| 3 | N  | 
| 4 | NULL | 
| 5 |  | 
+----+-------+ 
5 rows in set (0.00 sec) 

Così MySQL fa rispettare il valore di default, ma permette anche NULL. (È interessante notare che troncherà valori non validi e consentire le stringhe vuote pure, ma questo è un problema diverso)

6

per impostare un valore di default 'N' per la colonna con un vincolo NOT NULL, fare questo:

ALTER TABLE asdf ADD field ENUM('N', 'Y') NOT NULL; 

Spiegazione: Secondo il MySQL reference manual:

Se una colonna ENUM è dichiarata NOT NULL, il suo valore di default è il primo elemento della lista dei consentiti

0

Risposta da Ian Clelland è molto buona, ma maggiori dettagli:

mysql> CREATE TABLE en1(en enum('A','B') DEFAULT 'A',sets set('A','B') DEFAULT ' 
    B'); 
    Query OK, 0 rows affected (0.03 sec) 

    mysql> INSERT INTO en1 VALUES(); 
    Query OK, 1 row affected (0.00 sec) 

    mysql> INSERT INTO en1 VALUES(NULL,NULL); 
    Query OK, 1 row affected (0.00 sec) 

    mysql> SELECT * FROM en1; 
    +------+------+ 
    | en | sets | 
    +------+------+ 
    | A | B | 
    | NULL | NULL | 
    +------+------+ 
    2 rows in set (0.00 sec) 

    mysql> CREATE TABLE en2(en enum('A','B') DEFAULT 'A' NOT NULL,sets set('A','B') 
    DEFAULT 'B' NOT NULL); 
    Query OK, 0 rows affected (0.05 sec) 

    mysql> INSERT INTO en2 VALUES(NULL,NULL); 
    ERROR 1048 (23000): Column 'en' cannot be null 
Problemi correlati