Alter Column Data Type in MySQL

MySQL Alter Column DataType

A MySQL column's data type specifies the kind of information the column is intended to store. In other words, a column data type determines the operation that can performed on the column. And if you want to change the data type of a column then use a change statement with the column name. Basically if you change any column data type, the table is also altered by the process, so we say MySQL alters the data type and is used to modify the table and change the data type of the field.

Query for changing data type for a column

ALTER  TABLE table_name
CHANGE     column_name  column_name  new data_type;

Here I describe how to change a column's data type.

Query for creating a table

create-mysql-table-in-php.jpg
 
Using the above query, the login table has been created with the two fields (u_id and u_name).

Description of table login

descripition-of-login-table.jpg

Query to INSERT values into table name login

insert-into-table-in-php.jpg

After the execution of this query a value is inserted into the login table.

The following query image shows what will happen when you try to insert a value not supported by the column data type.

insert1-into-table-in-php.jpg

In the first query of the above image ( insert into login values (102y,'[email protected] )  ) In this the user tried to insert 102y in the column u_id, which is an int(10) data type. Thererfore the query generates an error. And in the next query of the above image ( insert into login values (102y,'[email protected] )  ) the user tried to insert '102y' in the column u_id. The data has been inserted into the u_is column, but excludes the character.

descripition1-of-login-table.jpg

Because you can see in the above image that the u_id data type is int(10).

Query to ALTER or CHANGE a ccolumn data type of the Table named login

alter-comumn-datatype-in-mysql.jpg

After altering the login table, you can again insert values with an appropriate varchar type of the u_id column into the login table. The query ( insert into login values (102y,'[email protected] )  ) does not generate an error.

select-values-from-sql-table.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all