2013-04-09 17 views
7

Perché il primo INSERT passa per table2. Si noti che table2.col_1 NON è NULL. Non inserisce NULL per col_1, ma converte misteriosamente il valore NULL in una stringa vuota. Sto usando MySQL versione 5.5.28. GrazieMySQL Inserisce record con valore NULL nella colonna NOT NULL

mysql> DROP TABLE IF EXISTS table1, table2; 

Query OK, 0 rows affected (0.01 sec) 

mysql> CREATE TABLE IF NOT EXISTS table1 (
    -> id INT UNSIGNED NOT NULL AUTO_INCREMENT , 
    -> col_1 VARCHAR(45) NOT NULL , 
    -> col_2 VARCHAR(45) NOT NULL , 
    -> PRIMARY KEY (`id`)) 
    -> ENGINE = InnoDB; 

Query OK, 0 rows affected (0.01 sec) 

mysql> CREATE TABLE table2 LIKE table1; 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO table1 (id, col_1, col_2) VALUES (NULL, "xxx","yyy"); 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO table2 (id, col_1, col_2) SELECT NULL, NULL, col_2 FROM table1 WHERE id=1; 
Query OK, 1 row affected, 1 warning (0.00 sec) 
Records: 1 Duplicates: 0 Warnings: 1 

mysql> SHOW WARNINGS; 
+---------+------+-------------------------------+ 
| Level | Code | Message      | 
+---------+------+-------------------------------+ 
| Warning | 1048 | Column 'col_1' cannot be null | 
+---------+------+-------------------------------+ 
1 row in set (0.00 sec) 

mysql> SELECT * FROM table2; 
+----+-------+-------+ 
| id | col_1 | col_2 | 
+----+-------+-------+ 
| 1 |  | yyy | 
+----+-------+-------+ 
1 row in set (0.00 sec) 

mysql> INSERT INTO table2 (id, col_1, col_2) VALUES(NULL, NULL, "zzz"); 
ERROR 1048 (23000): Column 'col_1' cannot be null 

mysql> SELECT * FROM table2; 
+----+-------+-------+ 
| id | col_1 | col_2 | 
+----+-------+-------+ 
| 1 |  | yyy | 
+----+-------+-------+ 
1 row in set (0.00 sec) 
+0

forse inserisce 'stringa NULL' e non il nulla (* nulla *) –

+0

Strano, sto facendo lo stesso e io capisco i valori nulli inseriti ... forse si potrebbe specificare PDO: PARAM_INT quando si associa il valore? – Sebas

+0

@ JW Si prega di spiegare. Ho appena modificato il post originale per mostrare come inserisce una riga solo se INSERT è alimentato da un SELECT. – user1032531

risposta

7

Hai la modalità STRICT di MySQL disattivata. Accendilo e riceverai un errore.

Altrimenti si può verificare questi avvertimenti con PDO tramite: http://php.net/manual/en/pdo.errorinfo.php

+0

Grazie a David, il mio male, risulta non avere nulla a che fare con PDO o PHP. Ho modificato il post originale per mostrarlo. Dispiace per la confusione. – user1032531

+0

@ user1032531 Aggiornato questa risposta per riflettere la mia ulteriore comprensione. – DavidScherer

+0

Scommetto che hai ragione! Non ho mai saputo della modalità MySQL STRICT, e lo testeremo. Grazie – user1032531

4

Questo comportamento è ben documentato nella documentazione di MySQL. MySQL doc

Se non si utilizza la modalità rigorosa, quindi ogni volta che si inserisce un valore “corretto” in una colonna, such as a NULL into a NOT NULL column o un troppo grande valore numerico in una colonna numerica, MySQL imposta la colonna per il “miglior valore possibile” invece di produrre un errore:, ma il conteggio degli avvisi viene incrementato

0

Ho provato a impostare OFF la modalità STRETTA di MySQL e non ha funzionato per me (l'ho persino modificato in "my.ini").

Quello che ha funzionato per me è stato un BEFORE INSERT TRIGGER

Fondamentalmente è fare:

CREATE TRIGGER triggerName BEFORE INSERT ON customer 
FOR EACH ROW 
BEGIN 
    if new.`customerName` = '' then 
    signal sqlstate '45000' 
    SET MESSAGE_TEXT = 'Customer Name Cannot be Empty!'; 
    end if; 
END 

Nel MESSAGE_TEXT è possibile aggiungere qualsiasi testo che si desidera l'errore di mostrare.

Spero che aiuti!

maggior parte di esso trovato here

Problemi correlati