How to update a column of varchar type in MySQL to increase its length?

MySQLMySQLi Database

Let us first create a table. Here, we have two columns with varchar type −

mysql> create table DemoTable
(
   UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserFirstName varchar(10),
   UserLastName varchar(20) ,
   UserAge int
);
Query OK, 0 rows affected (0.96 sec)

Let us check the description of table using DESC command −

mysql> desc DemoTable;

This will produce the following output −

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| UserId        | int(11)     | NO   | PRI | NULL    | auto_increment |
| UserFirstName | varchar(10) | YES  |     | NULL    |                |
| UserLastName  | varchar(20) | YES  |     | NULL    |                |
| UserAge       | int(11)     | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Following is the query to update a column of varchar type in MySQL to increase its length −

mysql> alter table DemoTable modify UserFirstName varchar(40);
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0

Let us check the description of table once again to check what we did above. We updated the length of varchar type −

mysql> desc DemoTable;

This will produce the following output −

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| UserId        | int(11)     | NO   | PRI | NULL    | auto_increment |
| UserFirstName | varchar(40) | YES  |     | NULL    |                |
| UserLastName  | varchar(20) | YES  |     | NULL    |                |
| UserAge       | int(11)     | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 12:27:24
Advertisements