How to Change a Column Size or Type in MySQL

Use ALTER TABLE and MODIFY commands to change a MySQL column

Man working on a laptop computer

courtneyk/Getty Images

Just because you made a MySQL column one type or size doesn't mean that it has to stay that way. Changing the column type or size in an existing database is simple.​

Changing a Database Column Size and Type

You change a column size or type in MySQL using the ALTER TABLE and MODIFY commands together to make the change. 

Let's say, for example, that you have a column named "State" on a table named "Address" and you previously set it up to hold two characters, expecting people to use 2-character state abbreviations. You find that several people entered entire names instead of 2-character abbreviations, and you want to allow them to do this. You need to make this column larger to allow the full state names to fit. Here is how you do it:

 

ALTER TABLE address MODIFY state VARCHAR(20) ;

In generic terms, you use the ALTER TABLE command followed by the table name, then the MODIFY command followed by the column name and new type and size. Here is an example:

 ALTER TABLE tablename MODIFY columnname VARCHAR(20) ;

The maximum width of the column is determined by the number in parentheses. The type is identified by VARCHAR as being a variable character field.

About VARCHAR

The VARCHAR(20) in the examples can change to whatever number is appropriate for your column. VARCHAR is a character string of variable length. The maximum length—in this example it is 20—indicates the maximum number of characters you want to store in the column. VARCHAR(25) could store up to 25 characters.

Other Uses for ALTER TABLE

The ALTER TABLE command can also be used to add a new column to a table or to remove an entire column and all its data from a table. For example to add a column, use:

 ALTER TABLE table_name
 ADD column_name datatype

To delete a column, use:

 ALTER TABLE table_name
 DROP COLUMN column_name
Format
mla apa chicago
Your Citation
Bradley, Angela. "How to Change a Column Size or Type in MySQL." ThoughtCo, Apr. 5, 2023, thoughtco.com/change-columns-size-type-in-mysql-2693875. Bradley, Angela. (2023, April 5). How to Change a Column Size or Type in MySQL. Retrieved from https://www.thoughtco.com/change-columns-size-type-in-mysql-2693875 Bradley, Angela. "How to Change a Column Size or Type in MySQL." ThoughtCo. https://www.thoughtco.com/change-columns-size-type-in-mysql-2693875 (accessed April 26, 2024).