May 28, 2024 By Matthew Rathbone

Changing a column type in MySQL is a common task when working with databases. You might need to change a column’s type to accommodate new data requirements or to optimize performance. This tutorial will explain how to change the column type using the ALTER TABLE statement, with practical examples.

Understanding the ALTER TABLE Statement

The ALTER TABLE statement is used to modify an existing table’s structure. It can add, delete, or change columns and indexes. When changing a column type, the basic syntax is:

ALTER TABLE table_name MODIFY COLUMN column_name new_data_type;

Let’s break down the syntax:

  • table_name: The name of the table containing the column you want to modify.
  • column_name: The name of the column you want to modify.
  • new_data_type: The new data type you want to assign to the column.

Example: Changing Column Type in MySQL

Consider a table named users with the following structure:

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50),
    age INT
);

You can check the initial structure using:

DESCRIBE users;

Expected output:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int         | NO   | PRI | NULL    | auto_increment |
| username | varchar(50) | YES  |     | NULL    |                |
| age      | int         | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

Changing age Column Type

Suppose you want to change the age column from INT to TINYINT because the range of ages is small and you want to save space.

ALTER TABLE users MODIFY COLUMN age TINYINT;

Running the above command will alter the age column to TINYINT. Verify the change:

DESCRIBE users;

Expected output:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int         | NO   | PRI | NULL    | auto_increment |
| username | varchar(50) | YES  |     | NULL    |                |
| age      | tinyint     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

Note that TINYINT can store a smaller range of numbers compared to INT. As you might imagine, this can cause problems with converting the data in the table. I cover the implications of this in the Conversion Issues section below.

Changing username Column Type

Now, let’s change the username column from VARCHAR(50) to VARCHAR(100) to allow longer usernames.

ALTER TABLE users MODIFY COLUMN username VARCHAR(100);

Verify the change:

DESCRIBE users;

Expected output:

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int          | NO   | PRI | NULL    | auto_increment |
| username | varchar(100) | YES  |     | NULL    |                |
| age      | tinyint      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

Handling Data Conversion Issues

When changing a column type, you might encounter issues if the existing data is incompatible with the new type. MySQL will try to convert the existing data, but if a value cannot be converted, it may result in NULL values or truncation.

Example: Data Truncation - INT to TINYINT

In our INT to TINYINT example there is potential for data truncation, because TINYINT doesn’t support the same range of values that INT does.

  • INT can store values from -2,147,483,648 to 2,147,483,647 (signed) or 0 to 4,294,967,295 (unsigned).
  • TINYINT can store values from -128 to 127 (signed) or 0 to 255 (unsigned).

This is what happens during conversion:

a. If the INT values in the column are within the range that TINYINT can store, they will be converted without issues.
b. If the INT values exceed the range of TINYINT, they will be truncated to fit within the TINYINT range. For example, if the INT value is 300, it will be truncated to 127 for signed TINYINT or 255 for unsigned TINYINT.

MySQL might generate warnings or errors during the conversion if the data exceeds the new data type’s limits, depending on the SQL mode settings.

Example: Changing to a Non-Compatible Type

Consider this scenario:

ALTER TABLE users MODIFY COLUMN age CHAR(3);

This changes the age column to CHAR(3). However, if any age exceeds 999, it will be truncated to the first three characters. Always ensure the new type can handle the existing data to avoid losing information.

Conclusion

Changing a column type in MySQL involves using the ALTER TABLE statement with the MODIFY COLUMN clause. Always verify changes and be cautious of data conversion issues. This tutorial has demonstrated how to perform this task with practical examples to ensure you can apply it in your projects confidently.

By understanding and using the ALTER TABLE statement, you can effectively manage and optimize your database structures in MySQL.

Beekeeper Studio Is A Free & Open Source Database GUI

Best SQL query & editor tool I have ever used. It provides everything I need to manage my database. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio is fast, intuitive, and easy to use. Beekeeper supports loads of databases, and works great on Windows, Mac and Linux.

Beekeeper's Linux version is 100% full-featured, no cut corners, no feature compromises.

More Mysql Articles