Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    int not null sulle colonne

    ho creato delle tabelle in cui le colonne hanno come valori degli int not null e dei text not null. Ho creato le colonne con dei not null, per evitare che i campi potessero rimanere vuoti e per prova ho creato un semplice form in cui inserire dei dati in questi campi. Per prova ho fatto un submit dei dati con tutti i campi vuoti e mi sarei aspettato un messaggio di errore da parte del server mysql poiché specificando i campi come not null ho pensato che il server mysql si aspettasse qualcosa che invece non é arrivato ma doveva arrivare.. come mai, invece, mysql ha permesso il submit creando così dei campi vuoti nella tabella se anche ho specificato che fossero not null?

  2. #2
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Una stringa vuota è diversa da null.

    Se provi a inserire un valore null mysql ti restituirà l'errore

    Column 'campo' cannot be null

    Se invece passi una stringa vuota e non è specificato un valore di default, myql ti restituirà un warning.

    codice:
    mysql> create table prova (
        -> id int not null auto_increment primary key,
        -> testo varchar(10),
        -> num int not null);
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> insert into prova (testo,num) values ('aaa',3);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into prova (testo) values ('aaa');
    Query OK, 1 row affected, 1 warning (0.00 sec)
    
    mysql> show warnings;
    +---------+------+------------------------------------------+
    | Level   | Code | Message                                  |
    +---------+------+------------------------------------------+
    | Warning | 1364 | Field 'num' doesn't have a default value |
    +---------+------+------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> insert into prova (testo,num) values ('aaa',null);
    ERROR 1048 (23000): Column 'num' cannot be null
    Quindi se vuoi che obbligatoriamente venga inserito un valore relativamente a un campo devi accertartene in fase di validazione dell'input.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.