The Effect of ALTER TABLE on Null and Default Value Attributes
Problem
You changed a column definition, but MySQL
modified the column’s NULL value
and default value attributes when you didn’t tell it
to.
Solution
Those attributes are part of the column definition. If you don’t specify them explicitly, MySQL chooses their values for you. So just be more specific about how you want the column defined.
Discussion
When you MODIFY or CHANGE a
column, you can also specify whether or not the column can contain
NULL values, and what its default value is. In
fact, if you don’t do this, MySQL automatically
assigns values for these attributes, with the result that the column
may end up defined not quite the way you intend. To see this, try the
following sequence of commands. First, modify j so
that it cannot contain NULL values and to have a
default value of 100, then see what SHOW
COLUMNS tells you:[36]
mysql>ALTER TABLE mytbl MODIFY j INT NOT NULL DEFAULT 100;mysql>SHOW COLUMNS FROM mytbl LIKE 'j';+-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | j | int(11) | | | 100 | | +-------+---------+------+-----+---------+-------+
So far, so good. Now if you were to decide to change
j from INT to
BIGINT, you might try the following statement:
mysql> ALTER TABLE mytbl MODIFY j BIGINT;However, that also undoes the NULL and
DEFAULT specifications of the previous
ALTER TABLE statement:
mysql> SHOW COLUMNS ...