Will extending the size of a varchar field in MySQL affect the data inside it?


If you will extend the size of a varchar field in MySQL then it won’t affect the data inside it.

Let us first create a table −

mysql> create table DemoTable(
   Name varchar(8)
);
Query OK, 0 rows affected (1.11 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Mike');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Name   |
+--------+
| John   |
| Robert |
| Mike   |
| Sam    |
+--------+
4 rows in set (0.00 sec)

Let us check the description of the table −

mysql> desc DemoTable;

This will produce the following output −

+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| Name  | varchar(8) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.04 sec)

Let us now extend the VARCHAR size from VARCHAR(8) to VARCHAR(100) −

mysql> alter table DemoTable change Name Name varchar(100);
Query OK, 4 rows affected (1.96 sec)
Records: 4 Duplicates: 0 Warnings: 0

Let us check the description of the table once again −

mysql> desc DemoTable;

This will produce the following output −

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| Name  | varchar(100) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.01 sec)

After extending the size of the varchar, the data inside it won’t get affected, but if the size is decreased then it can lead to issues.

Updated on: 27-Sep-2019

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements