
- 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
Change the column name from StudentName to FirstName in MySQL?
Use CHANGE with ALTER statement. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentName 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 | +----------------+--------------+------+-----+---------+-------+ | StudentName | varchar(100) | YES | | NULL | | | Age | int(11) | YES | | NULL | | +----------------+--------------+------+-----+---------+-------+ 2 rows in set (0.25 sec)
Here is the query with ALTER −
mysql> alter table DemoTable -> add column Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> add column CountryName varchar(100) after Age, -> change column StudentName 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
- Change the column name from a MySQL table with Student record?
- How can I change the name of an existing column from a MySQL table?
- Change a MySQL Column datatype from text to timestamp?
- How to extract column name and type from MySQL?
- How to change the length of the Name column from CHAR(20) to CHAR(50)?
- How to make 'from' as column name in MySQL?
- How to change MySQL column definition?
- Problem using the column name 'from' in a MySQL query?
- Rename column name in MySQL?
- How to change the name of single column using setNames in R?
- Change the file extension in the text column in MySQL?
- How to find the name of a column in MySQL?
- How to change the column position of MySQL table without losing column data?
- MongoDB query to get distinct FirstName values from documents
- Wildcards in column name for MySQL?

Advertisements