
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How do I modify a MySQL column to allow NULL?
For our example, let us create a table with NOT NULL constraint. After that, we will modify a column to allow NULL.
The following is the query to create a table with NOT NULL constraint.
mysql> create table AllowNullDemo -> ( -> id int not null -> ); Query OK, 0 rows affected (0.48 sec)=
Insert records with the help of INSERT command. 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)
The query to display records.
mysql> select *from AllowNullDemo;
Here is the output. The value 0 is displayed, since we haven’t added any value while using INSERT command above.
+----+ | id | +----+ | 0 | | 0 | +----+ 2 rows in set (0.00 sec)
Here is the syntax to allow NULL value.
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 NULL value to that column because the column is modified successfully above.
mysql> insert into AllowNullDemo values(); Query OK, 1 row affected (0.15 sec)
Display records to check whether the last value inserted is NULL or not.
mysql> select *from AllowNullDemo;
The following is the output wherein NULL value is visible now.
+------+ | id | +------+ | 0 | | 0 | | NULL | +------+ 3 rows in set (0.00 sec)
Using the above method, we can easily modify a MySQL column to allow NULL.
- Related Articles
- How do I update the decimal column to allow more digits in MySQL?
- How do I check if a column is empty or null in MySQL?
- How do I insert a NULL value in MySQL?
- How to modify column default value in MySQL?
- Using MySQL, can I sort a column but allow 0 to come last?
- How do I update NULL values in a field in MySQL?
- How do I alter a MySQL table column defaults?
- How to modify the size of column in MySQL table?
- How do I modify a string in place in Python?
- How to add a NOT NULL column in MySQL?
- How can we modify column/s of MySQL table?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- MySQL query to make a date column NULL?
- How do I select data that does not have a null record in MySQL?
- How can I modify an existing column's data type?
