How to Change a Column Size or Type in MySQL (2024)

Use ALTER TABLE and MODIFY commands to change a MySQL column

How to Change a Column Size or Type in MySQL (1)

By

Angela Bradley

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).

How to Change a Column Size or Type in MySQL (2024)

FAQs

How to Change a Column Size or Type in MySQL? ›

The basic syntax for changing a column type using the ALTER TABLE command: ALTER TABLE table_name MODIFY COLUMN column_name new_data_type; In this syntax: table_name is the name of the table you want to modify.

How to change column data type size in MySQL? ›

Changing a Database Column Size and Type

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.

How to modify the size of a column in SQL? ›

To change the data type, or the size of a table column we have to use the ALTER TABLE statement. The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

How do you change or modify a column in MySQL? ›

The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.

How do I change the name and type of a column in MySQL? ›

To rename a column in MySQL the following syntax is used: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; This command is used to change the name of a column to a new column name. However, renaming columns this way you run the risk of breaking database dependencies.

How to change column content in MySQL? ›

ALTER TABLE in MySQL | Modifying a Column in MySQL
  1. Add a column to a table using ALTER TABLE with ADD. ...
  2. Add Multiple columns to a table. ...
  3. Modify a column using ALTER TABLE with MODIFY. ...
  4. Rename columns using ALTER TABLE with CHANGE COLUMN. ...
  5. Drop a column using ALTER TABLE with DROP COLUMN.

How to change database column datatype in SQL? ›

Select the column for which you want to modify the data type. In the Column Properties tab, select the grid cell for the Data Type property and choose a new data type from the drop-down list. On the File menu, select Save table name.

How to check column size in SQL? ›

How to check column size in SQL? To get the length of a column in SQL Server, use the COL_LENGTH() function. More specifically, the function returns the column's defined length in bytes. The function takes two arguments: the name of the table and the name of the column.

Which command is used to change datatype of any column or to modify its size? ›

The ALTER COLUMN command is used to change the data type of a column in a table.

How to convert column type from int to VARCHAR in SQL? ›

The int_column can be changed into a VARCHAR datatype using the CONVERT function, which works similarly to CAST. The length of the resulting VARCHAR column is specified by (VARCHAR(10)). AS varchar_column assigns a name to the converted column in the result set.

What is the difference between modify column and change in MySQL? ›

MODIFY is simpler, but cannot change the column name. CHANGE is more confusing to use, but can change both the name and the definition.

How to edit column value in MySQL Workbench? ›

To change the name, data type, default value, or comment of a column, double-click the value to edit it. You can also add column comments to the Column Comment field. It is also possible to set the column collation, using the list in the Column Details panel.

How to change data structure in MySQL? ›

Changing the database structure with SQL queries. You can change the database structure with an ALTER query. The basic format for this query is ALTER TABLE tablename, followed by the specified changes.

How to alter column type in SQL Server? ›

SQL query to change the column type in SQL Server database
  1. Tbl_name: Specify the table name.
  2. Col_name: Specify the column name whose datatype you want to change. The col_name must be specified after the ALTER COLUMN keyword.
  3. Datatype: Specify the new datatype and length of the column.
Sep 22, 2021

How do I change the column name and type in SQL Server? ›

In MS SQL Server, you have to use the stored procedure called sp_rename. Syntax 1 sp_rename 'TableName. OldColumnName', 'New ColumnName', 'COLUMN'; Example: Write a query to rename the column name “BID” to “BooksID”. 1 sp_rename 'Books.

What is the use of alter in MySQL? ›

ALTER TABLE changes the structure of a table. For example, you can add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. You can also change characteristics such as the storage engine used for the table or the table comment.

How to convert column values to lowercase in MySQL? ›

The syntax for using LOWER in MySQL is straightforward. Simply include the keyword "LOWER" followed by an expression or column name within parentheses. The expression or column should contain the text string you want to convert to lowercase.

How to column data type in MySQL? ›

You can get the MySQL table columns data type with the help of "information_schema. columns".

How to check size of column in MySQL? ›

This can be accomplished easily with the following query: SELECT TABLE_SCHEMA AS `Database`, TABLE_NAME AS `Table`, ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)` FROM information_schema.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6596

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.