Bug #7574 Alter Table Modify column removes non modified constraints
Submitted: 29 Dec 2004 14:53 Modified: 29 Dec 2004 17:26
Reporter: karthick Srinivasan Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.0.20 OS:Linux (Linux)
Assigned to: CPU Architecture:Any

[29 Dec 2004 14:53] karthick Srinivasan
Description:
Dear Members ,

Step 1:  I created an table with an column namely ID having DEFAULT value of 5.
Step 2:  I altered same table modify column ID from null to not nullable. 

But after step 2 default value I have set in create table (in step 1) is reset to 0.  It seems that modify column of alter table requires entire column defintion even if column defintion given during create table is not modified. 

 My view is when I modify particular column I will think over the my constraint only I don't  think of redefining the entire column definition again,
If I am wrong kindly correct me,

Below is the list of sql commands and result I have got while I observed this behaviour,

mysql> create table MTEST(ID INTEGER DEFAULT 5 , SALARY INTEGER) type =InnoDB;
Query OK, 0 rows affected (0.20 sec)

mysql> insert into MTEST (SALARY) values(10000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from MTEST;
+------+--------+
| ID   | SALARY |
+------+--------+
|    5 |  10000 |
+------+--------+
1 row in set (0.00 sec)

mysql> desc MTEST;
+--------------+---------+------+-----+---------+-------+
| Field           | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-------+---------+-------+
| ID               | int(11) | YES  |        | 5       |       |
| SALARY  | int(11) | YES  |         | NULL    |       |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table MTEST MODIFY  COLUMN ID INTEGER NOT NULL;
Query OK, 1 row affected (0.21 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MTEST (SALARY) values(20000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from MTEST;
+----+--------+
| ID | SALARY |
+----+--------+
|  5 |  10000 |
|  0 |  20000 |
+----+--------+
2 rows in set (0.00 sec)

mysql> desc MTEST;
+----------------+---------+--------+-----+---------+-------+
| Field            | Type    | Null   | Key | Default | Extra |
+---------------+---------+--------+------+---------+-------+
| ID                | int(11) |           |         | 0         |       |
| SALARY    | int(11) | YES  |         | NULL|       |
+---------------+---------+--------+------+---------+-------+
2 rows in set (0.00 sec)

How to repeat:
 created  table with an column namely ID having DEFAULT value of 5.
Then  altered same table by modifying  column ID from null to not nullable.
check for table description
[29 Dec 2004 17:26] MySQL Verification Team
That is the behavior expected currently.
You need to use in the alter table statement the default clause again
for to preserve the default value like was done in create table.

Please see:

http://dev.mysql.com/doc/mysql/en/ALTER_TABLE.html
[31 Dec 2004 5:42] karthick Srinivasan
Could you consider this as feature request i.e. when modifying a column only modified properties needs to be changed other properties of the column need to be reatined.