Cosa assegnare ad una colonna MySQL che non deve essere vuota?
Definire con NOT NULL se una colonna non deve essere vuota. Creiamo prima una tabella con una delle colonne come NOT NULL :
mysql> create table DemoTable1895
(
Id int NOT NULL,
FirstName varchar(20),
LastName varchar(20) NOT NULL
);
Query OK, 0 rows affected (0.00 sec)
Inserisci alcuni record nella tabella utilizzando il comando di inserimento:
mysql> insert into DemoTable1895 values(100,'John','Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1895 values(NULL,'Chris','Brown');
ERROR 1048 (23000): Column 'Id' cannot be null
mysql> insert into DemoTable1895 values(102,'Carol',NULL);
ERROR 1048 (23000): Column 'LastName' cannot be null
mysql> insert into DemoTable1895 values(103,NULL,'Miller');
Query OK, 1 row affected (0.00 sec)
Visualizza tutti i record dalla tabella utilizzando l'istruzione select :
mysql> select * from DemoTable1895;
Ciò produrrà il seguente output:
+-----+-----------+----------+
| Id | FirstName | LastName |
+-----+-----------+----------+
| 100 | John | Smith |
| 103 | NULL | Miller |
+-----+-----------+----------+
2 rows in set (0.00 sec)