Use ALTER TABLE and MODIFY commands to change a MySQL column
By
Computer Science Expert
- B.A, History, Eastern Oregon University
Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms.
Learn about ourEditorial Process
Updated on September 19, 2018
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 theALTER TABLEandMODIFY 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 theMODIFY command followed by the column name and new type and size. Here is an example:
ALTER TABLE tablenameMODIFY columnnameVARCHAR(20) ;
The maximum width of the column is determined by the number in parentheses. The type is identifiedby 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 exampleit 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 TABLEcommand 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
mlaapachicago
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-2693875Bradley, 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 5, 2024).