SQL Query to Alter Column Size in MySQL
Last Updated :
07 Oct, 2024
In MySQL, managing the structure of a database is crucial for optimal data organization and integrity. One common task is altering the size of a column to accommodate changing the data requirements. The ALTER TABLE statement and the MODIFY clause provide a straightforward method to achieve this.
Here, we will show the process of modifying column sizes effectively. To Alter the Column size in MySQL use the ALTER TABLE statement with MODIFY Clause.
The ALTER TABLE statement allows users to add, delete, or modify columns in an existing table in MySQL.
Syntax
ALTER TABLE table_name
MODIFY column_name
data_type(new_length);
where,
- table_name: The name of the table you want to alter.
- column_name: The column whose size you wish to change.
- data_type: The data type of the column (e.g., VARCHAR, INT, etc.).
- new_length: The new size you want to assign to the column.
Example: Increasing Column Size in MySQL
Let us look at examples of how to alter column size in MySQL.
Step 1: Create a Sample Table
First, we will create a demo table on which we will alter the column length.
Let’s create a table with table_name; “student”
-- create a table
CREATE TABLE students (Sr_No integer,Name varchar(20), Gender integer);
Step 2: Check the Current Structure of the Table
DESCRIBE is used to describe something. Since in database, we have tables, that’s why we use DESCRIBE or DESC(both are the same) commands to describe the structure of a table.
Query:
DESCRIBE student;
OR
DESC student;
Using this command for the above table (in Xampp);
Output:

Step 3: Increase Column Size
Now change the size of the column using ALTER TABLE command with MODIFY clause.
Query:
ALTER TABLE student
MODIFY Name
varchar(50);
Step 4: Verify the Changes
To see the table structure, use Describe command:
DESCRIBE student;
Output:

Column size is altered from 20 to 50.
Example: Decreasing Column Size in MySQL
In some cases, you may need to reduce the size of a column to optimize storage or meet specific requirements. Let’s reduce the size of the Gender column from 11 to 9 characters.
Query:
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.
Conclusion
Altering the size of a column in MySQL is a simple process with the ALTER TABLE and MODIFY clauses. You’re either increasing or decreasing the column size, and these commands allow you to efficiently manage your database as your data requirements evolve. Always back up your data before making structural changes and carefully consider the impact of these alterations, especially when working with critical data.
Similar Reads
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
3 min read
How to Rename a Column in MySQL?
Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
4 min read
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
SQL Server ALTER TABLE ADD Column
The ALTER TABLE ADD is a Data Definition Language (DDL) command that is used to alter the structure of the table by adding an extra column based on the new requirement. Using ALTER TABLE ADD we can also add new constraints and also indexes for the table. With the help of this command, We can simply
2 min read
SQL Query to Update All Columns in a Table
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
2 min read
Change Primary Key Column in SQL Server
Primary Key refers to the column of a table that uniquely identifies a row in a table. It contains unique values and can not contain NULL values. For the purpose of the demonstration, we will be creating geeks for geeks table in a database called âgeeksâ. Step 1: Creating the database The database i
2 min read
How to Change a Column Name in SQL?
The ALTER TABLE statement in SQL is a powerful command used to modify the structure of an existing table without affecting its data. It enables changes like adding, dropping, renaming or altering columns in the table. Among these operations, altering a column with the CHANGE or RENAME command is com
3 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command
3 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from
4 min read