Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NOT NULL restriction from column in MySQL

Tags:

null

mysql

alter

How can I alter a column whose restricted to NOT NULL to accept NULL values?

like image 515
Aufwind Avatar asked Apr 18 '11 18:04

Aufwind


People also ask

How do I change NOT NULL column to allow nulls in MySQL?

alter table yourTableName modify column yourColumnName datatype; Apply the above syntax to modify the column to allow NULL. The query is as follows. After executing the above query, you can insert NULL value to that column because the column is modified successfully above.

How do I change a column constraint from not null to NULL in SQL?

All you need to do is to replace [Table] with the name of your table, [Col] with the name of your column and TYPE with the datatype of the column. Execute the command and you are allowed to use NULL values for the specified column. That is all it takes to switch between NULL and NOT NULL .

Can we delete NOT NULL column in SQL?

We can remove a NOT NULL constraint from a column of an existing table by using the ALTER TABLE statement.


2 Answers

just modify it and put in the old type and leave off the not null

alter table table_name modify column foo int;
like image 88
regality Avatar answered Nov 15 '22 17:11

regality


Assuming the table is table_name, column is column_name and its defined as varchar(200):

alter table table_name modify column column_name varchar(200) default null;
like image 26
lobster1234 Avatar answered Nov 15 '22 18:11

lobster1234