
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- How to change the length of the Name column from CHAR(20) to CHAR(50)?
- How to extract column name and type from MySQL?
- Change a MySQL Column datatype from text to timestamp?
- Rename column name in MySQL?
- How to make 'from' as column name in MySQL?
- How to change the name of single column using setNames in R?
- Problem using the column name 'from' in a MySQL query?
- How to change MySQL column definition?
- Wildcards in column name for MySQL?
- Display distinct column name in MySQL
- How to find the name of a column in MySQL?
- How to get column index from column name in Python Pandas?
- Change the file extension in the text column in MySQL?
Advertisements