EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials MariaDB Tutorial MariaDB rename column

MariaDB rename column

Updated June 5, 2023

MariaDB rename column

Introduction to MariaDB rename column

MariaDB provides different types of commands to the user; the rename column name is one type of command. We want to change the structure of the existing table, meaning the user needs to change the table’s column name. We use the alter table command to change the specified column name at that time. Using the alter table command, we may do a variety of things, such as add and delete columns, etc. When we need to change the column name of the specified table, we execute the ALTER TABLE statement, but it requires at least ALTER privilege for the database or table. The alter table comes under the DDL language. In this topic, we will learn about the MariaDB rename column.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

alter table table_name change existing colm name colm definition new colm name with data types;

Explanation

In the above syntax, we used the alter table command to rename the column name; here, table_name is used for the specified table name we need to alter. After that, we write the changing keyword to rename the column name followed by the existing column name that we need to rename; here, we use the existing column name with a new column name with the data type we need to update.

How to rename a column in MariaDB?

Let’s see how the rename column name works in MariaDB as follows.

The first user must have altered table privileges to rename the column name from the specified table.

For the rename column, we use alter table command, by using alter table command we can change the structure of the specified table. First, we need to see the structure table we need to alter using the command. Then we use the above syntax to rename the column name with the column definition. When clauses are used IF EXISTS and IF NOT EXISTS, queries will not show the error message when the condition is triggered for that clause. It offers a warning message, and the alter command will move on to the next clause in the statement.

The change is used to rename the column from the specified table. In this clause, we can also change the data type of the column, and in this clause, the data type is mandatory even if we need to keep the existing data type. Another essential function of the change clause is placing the column in a different position using the first or after column clause as per our requirement.

Another way to rename columns is that rename column clauses; it is the simplest way to rename column names. We cannot rename column names that are already present in the table. The rename column clause can be used only to rename column names; if we need more functions, such as changing the data definition and position of the column, we need the change clause instead of the column clause.

Examples

Let’s see the different examples of rename columns as follows.

First, see the table structure by using the following statement.

describe emp;

Explanation

In the above statement, we use describe command to see the detailed structure of the emp table. It is helpful to see all the column names with its definition. The final output of the show databases queries we illustrate by using the following snapshot.

MariaDB rename column output 1

After that, the user can select any column name we need to rename as follows.

alter table emp change emp_id id int;

Explanation

In the above example, we use the alter table command to rename the column name, here emp the specified table name that we need to alter; after that, we use the change keyword to rename the column name. In this example, emp_id is the old column name we need to rename by id with data type integer, as shown in the above statement. The final output of the show databases queries we illustrate by using the following snapshot.

MariaDB rename column output 2

Now see the updated column name by using the following statement as follows.

select * from emp;

Explanation

In the above example, we use a select clause to see the whole table; see here, we successfully rename the column name; that is, we rename the column name emp_id to id with the integer data type. The final output of the show databases queries we illustrate by using the following snapshot.

MariaDB rename column output 3

We can also rename column names by using the rename keyword as follows.

Syntax

alter table table_name rename colm existing column name to new column name;

Explanation

In the above syntax, we used alter table command to rename the column name; here, the table name means a specified table that we need to alter; after that, we use rename keyword to update the column name. In the above syntax existing column name means the old column name that we need to rename by the new column name.

Example

alter table emp rename column emp_name to name;

Explanation

In the above example, we use the alter table command to update the emp table; in this example, we use rename column keyword instead of changing the keyword to rename column name. Here we rename emp_name to name as shown in the above statement. The final output of the show databases query we illustrate by using the following snapshot.

output 4

Now see the updated column name by using the following statement.

select * from emp;

Explanation

In the above example, we use a select clause to see the whole table; see here, we successfully rename the column name by using rename column keyword; that is, we rename the column name emp_name to name with the varchar data type. The final output of the show databases queries we illustrate by using the following snapshot.

output 5

Let’s see some other examples of rename columns as follows.

Suppose we need to change the emp_dept column name and place it after emp_address; we can use the following statement at that time.

alter table emp change emp_dept dept varchar(250) after emp_address;

Explanation

We use the change clause with the after option in the above example. The final output of the show databases query we illustrate by using the following snapshot

output 6

Conclusion

We hope from this article, you have understood the MariaDB rename column. From this article, we have learned the rename column’s basic syntax and see different examples rename columns. This article taught us how and when to use MariaDB rename columns.

Recommended Articles

We hope this EDUCBA information on “MariaDB rename column” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MariaDB Commands
  2. MariaDB UPDATE
  3. What is MySQL?
  4. MariaDB Foreign Key
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MARIADB Course Bundle - 5 Courses in 1
 11+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

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

Forgot Password?

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