How to Change Column Size in MySQL

Sometimes you may need to change column size or change field length in MySQL. In this article, we will look at how to change column size in MySQL. You can use these commands to increase column size in MySQL or decrease it.


How to Change Column Size in MySQL

Here is how to increase field length in MySQL. Let us say you have a VARCHAR column with length 20, and want to increase its length to 255.

In this case, you need to use ALTER TABLE statement to increase column size.

Here is the syntax for it

ALTER TABLE table_name
MODIFY column_name
varchar(new_length);

In the above command, you need to specify table_name whose column you want to modify, column_name of column whose length you want to change, and new_length, new size number.

Also read : How to Store JSON data in MySQL

Here is an example to increase column size in MySQL

mysql> create table sales(
       id int, 
       product_name varchar(20), 
       order_date date
       );
mysql> describe sales;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id           | int(11)     | YES  |     | NULL    |       |
| product_name | varchar(20) | YES  |     | NULL    |       |
| order_date   | date        | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

Also read : How to Compare Null Values in MySQL

Let us increase size of product_name from varchar(20) to varchar(255).

mysql> alter table sales
       modify product_name varchar(255);

mysql> describe sales;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | YES  |     | NULL    |       |
| product_name | varchar(255) | YES  |     | NULL    |       |
| order_date   | date         | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

Hopefully, this article will help you change column size in MySQL. Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.

mm

About Ubiq

Ubiq is a dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build dashboards & reports for your business. Try it for free today!