[vox-tech] Altering Columns in MySQL on RedHat

Marc Elliot Hall vox-tech@lists.lugod.org
Tue, 02 Dec 2003 09:34:57 -0800


(replying to self for benefit of the archive)

Marc Elliot Hall wrote:

> I've got a RedHat 8 box running MySQL 3.23.55. One of my tables has a 
> field with 'varchar(250)' as the data type.
> 
> mysql> desc foo;
> +-------------+--------------+------+-----+---------+----------------+
> | Field       | Type         | Null | Key | Default | Extra          |
> +-------------+--------------+------+-----+---------+----------------+
> | id          | int(11)      |      | UNI | NULL    | auto_increment |
> | sku         | varchar(10)  |      | PRI |         |                |
> | bar         | varchar(250) |      | MUL |         |                |
> | name        | text         | YES  |     | NULL    |                |
> | etc...      | int(11)      | YES  |     | NULL    |                |
> 
> 
> Unfortunately, 250 characters is insufficient for the data I want to put 
> into this field. So, I want to change it to 'blob' or 'text'. However, 
> when I use:
> 
> mysql> alter table foo modify bar text not null;
> 
> I receive back the error:
> 
> ERROR 1170: BLOB column 'bar' used in key specification without a key 
> length
> 
> I've tried adding a new column:
> 
> mysql> alter table foo add barnew text not null;
> Query OK, 19543 rows affected (3.84 sec)
> Records: 19543  Duplicates: 0  Warnings: 0
> 
> and the new column shows up:
> 
> | barnew      | text         |      |     |         |                |
> +-------------+--------------+------+-----+---------+----------------+
> 
> However, I have thus far been unable to get the values out of 'bar' and 
> into 'barnew'; I'm pro'ly just being boneheaded about this and missing 
> something obvious. :^)
> 
> My question is this: how do I change the data type of a column that's 
> part of the key? Google and http://www.mysql.com/doc/ have been (so far) 
> not very helpful. Any advice is appreciated!

Thanks to Tim Riley, David Hummel, and Jeff Newmiller for their input!

This is what I wound up doing (Looks like David was closest):

mysql> drop index bar on foo;
mysql> alter table foo modify bar text not null;
mysql> create index bar_index on foo (bar( 250 ));

Found a little extra clue here:

http://www.mysql.com/doc/en/CREATE_INDEX.html