319

I have created a table and accidentally put varchar length as 300 instead of 65353. How can I fix that?

An example would be appreciated.

informatik01
  • 16,038
  • 10
  • 74
  • 104
vehomzzz
  • 42,832
  • 72
  • 186
  • 216

3 Answers3

602

Have you tried this?

ALTER TABLE <table_name> MODIFY <col_name> VARCHAR(65353);

This will change the col_name's type to VARCHAR(65353)

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • glad to help. @Bill suggested using CHANGE instead of MODIFY which will work but CHANGE also renames the column so you have to put the column name twice.. – Mike Dinescu Aug 14 '09 at 19:07
  • 18
    Does this keep the data intact? – Flimm Jul 29 '13 at 13:51
  • 1
    @Flimm seems to for me. – deed02392 Dec 26 '13 at 21:44
  • 50
    @Flimm - just a pointer, if you have a VARCHAR(100) and you change it to VARCHAR(50) it will cut any existing data from columns. As per this specific question though, making a column larger won't have an issue with the data. – Warren Sergent Jan 31 '14 at 03:46
  • 9
    @WarrenSergent, tested in 5.7.15, it will throw an error if there are values affected by the change. It will not truncate by default. You will have to update the values beforehand with a SUBSTR. – Robert T. Nov 04 '16 at 13:36
  • 1
    @animo is right. Look at this for a complete answer http://stackoverflow.com/a/9611293/1594933 – gontard Nov 10 '16 at 11:21
  • what happens when you upgrade from Mysql 5.6 to 5.7, will the setting will remains same or we need to handles this type of case separately. Here setting i means is, varchar setting , for example default column varchar set to 100, but i changed to 500 after some time and post upgrade to 5.7 will the setting remains same. – user149621 Oct 21 '21 at 17:23
  • Yes, upgrading to 5.7 won't revert the column size to default on an existing column – Mike Dinescu Oct 21 '21 at 18:55
27
ALTER TABLE <tablename> CHANGE COLUMN <colname> <colname> VARCHAR(65536);

You have to list the column name twice, even if you aren't changing its name.

Note that after you make this change, the data type of the column will be MEDIUMTEXT.


Miky D is correct, the MODIFY command can do this more concisely.


Re the MEDIUMTEXT thing: a MySQL row can be only 65535 bytes (not counting BLOB/TEXT columns). If you try to change a column to be too large, making the total size of the row 65536 or greater, you may get an error. If you try to declare a column of VARCHAR(65536) then it's too large even if it's the only column in that table, so MySQL automatically converts it to a MEDIUMTEXT data type.

mysql> create table foo (str varchar(300));
mysql> alter table foo modify str varchar(65536);
mysql> show create table foo;
CREATE TABLE `foo` (
  `str` mediumtext
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

I misread your original question, you want VARCHAR(65353), which MySQL can do, as long as that column size summed with the other columns in the table doesn't exceed 65535.

mysql> create table foo (str1 varchar(300), str2 varchar(300));
mysql> alter table foo modify str2 varchar(65353);
ERROR 1118 (42000): Row size too large. 
The maximum row size for the used table type, not counting BLOBs, is 65535. 
You have to change some columns to TEXT or BLOBs
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • +1 you're guys have the same answer. what does it mean data type of the column will be MEDIUMTEXT? thx – vehomzzz Aug 14 '09 at 19:06
  • 4
    @Bill: CHANGE is generally used to rename a column and change it's data type. MODIFY will only change the column's data type – Mike Dinescu Aug 14 '09 at 19:08
0
ALTER TABLE {table_name} MODIFY [COLUMN] {column_name} {column_type} {defaults and/or not-null};

(Including COLUMN is optional.)

Note: if your column was created with NOT NULL etc. you may need to specify those in the MODIFY statement to avoid losing them.

  • Kindly add more details to your answer, explanations on how your code works and how this address the OP's question would be a great help not just for the asker but also for the future researchers. – Kuro Neko May 31 '22 at 05:55