Updated May 24, 2023
Introduction to ALTER Column in MySQL
The MySQL ALTER COLUMN query is a MySQL statement responsible for changing a table’s structure, either for adding a table column, altering the column, renaming the column, removing the column, or renaming the table itself.
MySQL ALTER COLUMN command allows the admin to modify an existing table by adding one or multiple columns. Also, the ALTER COLUMN can be used for dropping the column present in the table. This is useful for providing a new name to the current table column using a renaming query.
For changing the name of a table available in the database, we need to perform the query in the MySQL server having ALTER TABLE statement.
How to ALTER a Column in MySQL?
Let us explain the working of ALTER COLUMN in MySQL using the syntaxes and examples. Suppose we will set up a demo table in the database for demonstration, using the following CREATE query statement:
CREATE TABLE Products (ProductID INT PRIMARY KEY AUTO_INCREMENT, Product_Name VARCHAR (255) NOT NULL,SupplierID INT NOT NULL);
Output:
1. MySQL ALTER ADD COLUMN Query
We will use the ALTER TABLE ADD command to add one or more table columns to this demo table created above.
- Adding a single column to the table.
Let us add a column to the Products table using the succeeding syntax for ALTER TABLE ADD Query.
Query:
ALTER TABLE TableName
ADD NewColumnNameCol_Definition[ FIRST | AFTER ColumnName]
Here, the above terms are explained as follows:
- TableName: We will provide the specific table name after the keywords ALTER TABLE where we want to supplement a new column or columns.
- NewColumnName: Mention the name of the new table column to be added.
- Col_Definition: It includes the Data Type for the new column added, maximum size, and valid CONSTRAINTS.
- FIRST | AFTER ColumnName: It determines the position of the new table column to be added. There are two options: add the table column after the last column existing in the table using AFTER ColumnNameKeyword or, as the first column using FIRST ColumnNamekeyword. But if these options are not provided in the ALTER ADD COLUMN query, the new table column will be affixed at the end of the list of columns in the table.
Now, we will apply the ALTER TABLE ADD COLUMN statement to add a new column at the end of the columns present in the Products table in the database:
ALTER TABLE Products ADD Cost INT NOT NULL;
You can view the column lists of the Products table using the query below in the database:
Query:
DESCRIBE Products;
Output:
As you can see in the output above that a new column, Price, is now added to the Products table at the end of the lists.
- Adding multiple columns to the table.
Let us add multiple columns to the Products table using the succeeding syntax for ALTER TABLE ADD Query:
Query:
ALTER TABLE TableName
ADD NewColumnNameCol_Definition[ FIRST | AFTER ColumnName],
ADD NewColumnNameCol_Definition[ FIRST | AFTER ColumnName],
…………..;
Suppose we are adding two more columns in the Products table which is shown below with the help of ALTER query:
ALTER TABLE Products
ADD Unit VARCHAR(100) NOT NULL,
ADD DescriptionVARCHAR(255) ;
You can view the column lists of the Products table using the query below in the database:
Query:
DESCRIBE Products;
Output:
You can see a new structure of the table formed using the ALTER TABLE ADD COLUMN statements.
2. MySQL ALTER MODIFY COLUMN Query
We will use the ALTER TABLE MODIFY command to make some changes in the structure of existing columns in the Products table.
- Modifying a Single Table Column.
Here, we have the basic syntax describing the ALTER TABLE MODIFY statement and showing examples accordingly:
ALTER TABLE TableName
MODIFY ColumnNameCol_Definition[FIRST | AFTER ColumnName];
It is a virtuous practice to study or view the attributes associated with a column in a table before we modify using the ALTER query.
For example, we want to alter the column Description to have a NOT NULL column definition and VARCHAR characters’ length to 200.
Before the Structure of the Table:
After Modifying the Column:
ALTER TABLE Products
MODIFY Description VARCHAR(200) NOT NULL;
View the changes with altered maximum size and CONSTRAINT
Query:
DESCRIBE Products;
Output:
- Modifying Multiple table Columns
We have a similar syntax describing the ALTER TABLE MODIFY statement:
Syntax:
ALTER TABLE TableName
MODIFY ColumnNameCol_Definition [FIRST | AFTER ColumnName],
MODIFY ColumnNameCol_Definition [FIRST | AFTER ColumnName], …..;
Let us first change the data type of SupplierID from INT to SMALLINT and then, in a Unit column, change its maximum size to 50 and also remove the NOT NULL constraint by changing its position to appear after SupplierID:
Query:
ALTER TABLE Products
MODIFY SupplierIDSMALLINT,
MODIFY Unit VARCHAR(50) NULL AFTER SupplierID;
See the changes applied:
Query:
DESCRIBE Products;
Output:
3. MySQL ALTER RENAME COLUMN Query
We will use the following statement to rename a table column:
Syntax:
ALTER TABLE TableName
CHANGE COLUMN OriginalNameNewColumnNameCol_Definition[FIRST | AFTER ColumnName];
In the above syntax, the original name defines the column name that exists in the table, and NewColumnNamedefines the new name to be given.
For this, let us illustrate the following example:
Query:
ALTER TABLE Products
CHANGE COLUMN DescriptionProduct_InfoVARCHAR(255) NULL;
Here, we have changed the Description column name to the Product_Info name.
Query:
DESCRIBE Products;
Output:
4. MySQL ALTER DROP COLUMN Query
If we want to remove any table column in the database table, we will implement the DROP COLUMN command with an ALTER statement. Let us review the elementary syntax for dropping the column:
Syntax:
ALTER TABLE TableName
DROP COLUMN ColumnName;
Suppose we apply this as a query to the products table created above. Here is an example that demonstrates how to remove a column from the “products” table:
ALTER TABLE Products DROP COLUMN Product_Info;
View the changes in the Products table:
Query:
DESCRIBE Products;
Output:
Conclusion
- Hence, the MySQL ALTER COLUMN performs the query to add, modify, delete, or rename any columns in a table in the database server.
- The ALTER COLUMN IS also helpful in removing or adding various MySQL CONSTRAINTS associated with the present table. We can also rename a table using the ALTER TABLE command.
Recommended Articles
We hope that this EDUCBA information on the “ALTER Column in MySQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.