
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to alter the data type of a MySQL table’s column?
You can use modify command for this. Let us first us create a table.
mysql> create table DemoTable ( StudentId varchar(200) not null, StudentName varchar(20), StudentAge int, StudentAddress varchar(20), StudentCountryName varchar(20) ); Query OK, 0 rows affected (0.73 sec)
Now check the description of table.
mysql> desc DemoTable;
This will produce the following output −
+--------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | StudentId | varchar(200) | NO | | NULL | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(20) | YES | | NULL | | | StudentCountryName | varchar(20) | YES | | NULL | | +--------------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
Here is the query to alter the data type from varchar to BIGINT −
mysql> alter table DemoTable modify StudentId BIGINT(10) UNSIGNED NOT NULL DEFAULT 0; Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates : 0 Warnings : 0
Above, we have changed the column ‘StudentId’ datatype from varchar(200) to BIGINT(10). Let us check the description of table once again.
mysql> desc DemoTable;
This will produce the following output −
+--------------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+---------------------+------+-----+---------+-------+ | StudentId | bigint(10) unsigned | NO | | 0 | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(20) | YES | | NULL | | | StudentCountryName | varchar(20) | YES | | NULL | | +--------------------+---------------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
- Related Articles
- How do I alter a MySQL table column defaults?
- How to alter column type of multiple columns in a single MySQL query?
- How can we change the data type of the column in MySQL table?
- Alter a table column from VARCHAR to NULL in MySQL
- How to use ALTER TABLE statement for changing the size of a column in MySQL?
- How to alter the database engine of a MySQL database table?
- How to change the column position of MySQL table without losing column data?
- Alter a MySQL column to be AUTO_INCREMENT?
- How to repeat the values stored in a data column of MySQL table?
- How can we modify column/s of MySQL table?
- How do I alter table column datatype on more than 1 column at a time in MySql?
- How MySQL CONCAT() function, applied to the column/s of a table, can be combined with the column/s of other tables?
- How to add column using alter in MySQL?\n
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- Add a new value to a column of data type enum in MySQL?

Advertisements