Introduction to ALTER Column in MySQL
- The MySQL ALTER COLUMN query is a MySQL statement that is responsible to change the structure of a table, either for adding a table column, altering the column, renaming the column, removing the column or renaming the table itself.
- Basically, MySQL ALTER COLUMN command allows the admin to modify an existing table with the addition of one or multiple columns to it. 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 by using a renaming query.
- For changing the name of a table available in the database we need to perform the query in 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 proceed towards adding one or more table columns to this demo table just 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 also valid CONSTRAINTS for the column.
- FIRST | AFTER ColumnName: It determines the position of the new table column to be added. There are two options you can either add the table column after the last column existing in the table using AFTER ColumnNameKeyword or, as first column using FIRST ColumnNamekeyword. But if these options are not provided in the ALTER ADD COLUMN query then, 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 to a column in a table before we perform any modification using the ALTER query.
For example, we want to alter the column Description to have a NOT NULL column definition along with VARCHAR length of characters to 200.
Before the Structure of the Table:
After Modifying the Column:
ALTER TABLE Products
MODIFY Description VARCHAR(200) NOT NULL;
3. 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 by changing its maximum size to 50 and also removing the NOT NULL constraint with 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 Product_Info name.
Query:
DESCRIBE Products;
Output:
4. MySQL ALTER DROP COLUMN Query.
If we want to remove any table column present in the database table then, 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. The example for this is given as follows which shows how to remove the column that exists in 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 that exist 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
This is a guide to ALTER Column in MySQL. Here we discuss the Introduction of ALTER Column in MySQL and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –
- Introduction to MySQL Operators
- Top 23 MySQL String functions
- MySQL vs SQLite | Top 14 Comparisons
- Guide to MySQL Timestamp
16 Online Courses | 11 Hands-on Projects | 70+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses