
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Can we use ADD and CHANGE with ALTER Statement in MySQL?
Yes, we can use ADD and CHANGE with ALTER statement. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.84 sec)
Now check the description of table.
mysql> desc DemoTable;
Output
This will produce the following output −
+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | Name | varchar(100) | YES | | NULL | | | Age | int(11) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.25 sec)
Here is the query with ALTER to update a column name as well adding new columns −
mysql> alter table DemoTable -> add column Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> add column CountryName varchar(100) after Age, -> change column Name FirstName varchar(200); Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates: 0 Warnings: 0
Now check the description of table once again −
mysql> desc DemoTable;
Output
This will produce the following output −
+-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | FirstName | varchar(200) | YES | | NULL | | | Age | int(11) | YES | | NULL | | | CountryName | varchar(100) | YES | | NULL | | | Id | int(11) | NO | PRI | NULL | auto_increment | +-------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we change MySQL user password by using the ALTER USER statement?
- How can we use SIGNAL statement with MySQL triggers?
- How can we use a MySQL subquery with INSERT statement?
- How can we alter table to add MySQL virtual GENERATED COLUMNS?
- How can we alter table to add MySQL stored GENERATED COLUMNS?
- Can we alter order of columns in MySQL?
- Can we use SELECT NULL statement in a MySQL query?
- Can we use WHERE clause inside MySQL CASE statement?
- Can we use Switch statement with Strings in java?
- How can we use MySQL ALTER TABLE command for adding comments on columns?
- How can we alter a MySQL stored procedure?
- How can we alter a MySQL stored function?
- How can we change MySQL user password by using UPDATE statement?
- Can we add minutes to VARCHAR datetime records while applying INSERT statement in MySQL?
- Can we use backticks with column value in MySQL?

Advertisements