By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

How to change MySQL table column from NULL to NOT NULL

Short tutorial showing you how to quickly change a MySQL table column from NULL to NOT NULL.

Edited: 2015-08-28 16:01

This is just a quick tip and reminder as of how to change a column in a MySQL table from NULL to NOT NULL. Changing a column is done by using ALTER TABLE together with MODIFY, a working example is included below:

ALTER TABLE mytable MODIFY mycolumn BIGINT(20) NOT NULL;

Changing from NULL to NOT NULL from PHP

The above command can be entered in a terminal after logging in on your MySQL server. Alternatively, you can also send the query from PHP, in which case you would likely want to do something like below:

$this->query("ALTER TABLE mytable MODIFY mycolumn BIGINT NOT NULL");

The above PHP code is object oriented, if you are getting errors, or do not know what the "$this->" part is, then you likely don't need it.