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.
SQL ALTER TABLE Statement:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
Syntax:
ALTER TABLE table_name MODIFY column_name varchar(new_length);
Example:
Let’s create a table with table_name; “student”
-- create a table CREATE TABLE students (Sr_No integer,Name varchar(20), Gender integer);
DESCRIBE is used to describe something. Since in database we have tables, that’s why we use DESCRIBE or DESC(both are same) command to describe the structure of a table.
Syntax:
DESCRIBE student; OR DESC student;
Using this command for an above table (in Xampp);
Output:
Now change the size of the column using Alter command
ALTER TABLE student MODIFY Name varchar(50);
To see the table structure, use Describe command:
DESCRIBE student;
Output:
Column size is altered from 20 to 50.
For decreasing size of Column “Gender”
ALTER TABLE student MODIFY Gender varchar(9);
To see the table structure, use Describe command:
DESCRIBE student;
Output:
Column size is altered from 11 to 9.