Alter Table Modify Column MySQL

Profile picture for user arilio666

The ALTER TABLE MODIFY command modifies the column definition of the specified column name based on direct column name input or the position of the column.

Syntax

ALTER TABLE table_name
  MODIFY column_name column_definition
    [ FIRST | AFTER column_name ];

column_definition: Place to enter the modified preferred datatype or new data type that we want to assign and define the column as NULL or NOT NULL.

[ FIRST | AFTER column_name ]: This is optional as this add-on will tell MySQL onto which next column it should modify.

1. Modify a single column

ALTER TABLE crypto_price_list
  MODIFY Name varchar(100) NULL;

mysql Modifying a single column

As we can see, the column data type and definition are now modified to varchar(100) and NULL definition.

2. Modify Multiple Columns

ALTER TABLE table_name
  MODIFY column_name column_definition
    [ FIRST | AFTER column_name ],
  MODIFY column_name column_definition
    [ FIRST | AFTER column_name ],
  ...

Same as the first followed by a comma can add multiple column names with preferred data type and definition to modify.

ALTER TABLE crypto_price_list
  MODIFY Name varchar(100) NULL
  AFTER ID, MODIFY Alt_Coin varchar(100) NOT NULL;

mysql Modify Multiple Columns

Now we can see here the datatype and definition of the two columns have been modified.

Tags