Updated June 5, 2023
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.
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.
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.
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.
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.
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.
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
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.