For our example, let's create a table with a NOT NULL constraint. After that, we'll modify a column to allow NULL.
The following is the query to create a table with NOT NULL constraints.
mysql> create table AllowNullDemo -> ( -> id int not null -> ); Query OK, 0 rows affected (0.48 sec)=
Use the INSERT command to insert records. The query is as follows.
mysql> insert into AllowNullDemo values(); Query OK, 1 row affected, 1 warning (0.19 sec) mysql> insert into AllowNullDemo values(); Query OK, 1 row affected, 1 warning (0.15 sec)
Display recorded query.
mysql> select *from AllowNullDemo;
This is the output result. Since we did not add any value when using the INSERT command above, the value 0 is displayed.
+----+ | id | +----+ | 0 | | 0 | +----+ 2 rows in set (0.00 sec)
This is the syntax that allows NULL values.
alter table yourTableName modify column yourColumnName datatype;
Apply the above syntax to modify the column to allow NULL. The query is as follows.
mysql> alter table AllowNullDemo modify column id int; Query OK, 0 rows affected (1.59 sec) Records: 0 Duplicates: 0 Warnings: 0
After executing the above query, you can insert a NULL value into the column because the column has been successfully modified in the above operation.
mysql> insert into AllowNullDemo values(); Query OK, 1 row affected (0.15 sec)
Display records to check if the last inserted value is NULL.
mysql> select *from AllowNullDemo;
The following is the output, where NULL values are now visible.
+------+ | id | +------+ | 0 | | 0 | | NULL | +------+ 3 rows in set (0.00 sec)
Using the above method, we can easily modify the MySQL column to allow NULL.
The above is the detailed content of How to modify a MySQL column to allow NULL?. For more information, please follow other related articles on the PHP Chinese website!