EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial ALTER Column in MySQL 
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

ALTER Column in MySQL 

By Aanchal SharmaAanchal Sharma

ALTER Column in MySQL 

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);

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Output:

ALTER Column in MySQL Example 1

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:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)
  • 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:

ALTER Column in MySQL Example 2

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:

ALTER Column in MySQL Example 3

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:

ALTER Column in MySQL Example 4

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:

Products Table Example 5

  • 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:

ALTER Column in MySQL Example 6

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:

Products Table Example 7

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:

Products Table Example 8

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 –

  1. Introduction to MySQL Operators
  2. Top 23 MySQL String functions
  3. MySQL vs SQLite | Top 14 Comparisons
  4. Guide to MySQL Timestamp
Popular Course in this category
MS SQL Training (16 Courses, 11+ Projects)
  16 Online Courses |  11 Hands-on Projects |  70+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
PL SQL Training (4 Courses, 2+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more