Alter a table column from VARCHAR to NULL in MySQL


To alter, use the ALTER command with CHANGE as in the below syntax −

alter table yourTableName change yourColumnName yourColumnName datatype NULL DEFAULT NULL;

Let us first create a table −

mysql> create table DemoTable1356
    -> (
    -> FirstName varchar(30)
    -> );
Query OK, 0 rows affected (0.56 sec)

Let us implement the above syntax to alter a table column to NULL −

mysql> alter table DemoTable1356 change FirstName FirstName varchar(30) NULL DEFAULT NULL;
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

Insert some records in the table using insert command −

mysql> insert into DemoTable1356 values('Adam');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1356 values('John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1356 values();
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1356 values('Bob');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1356 values(NULL);
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1356;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Adam      |
| John      |
| NULL      |
| Bob       |
| NULL      |
+-----------+
5 rows in set (0.00 sec)

Updated on: 05-Nov-2019

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements