Introduction to MariaDB rename column
MariaDB provides different types of commands to the user; the rename column name is the one type command. We want to change the structure of the existing table that means the user needs to change the column name of the table. At that time we use the alter table command to change the specified column name. By using alter table command we can perform different operations such as add column and delete column etc. When we need to change the column name of the specified table that means we executing 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 are going to 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 that we need to alter. After that we write the change 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 that 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 alter 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 that we need to alter by using the command. Then we use the above syntax to rename the column name with 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 just shows 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 column, and in this clause, the data type is mandatory even if we need to keep the existing data type. Another most important function of the change clause is that we can place the column in a different position by using the first or after column clause as per our requirement.
Another way to rename columns is that rename column clause; 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 at that time we need the change clause instead of rename 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 useful 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 that 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 that 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 change 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 also we need to place after emp_address at that time we can use the following statement.
alter table emp change emp_dept dept varchar(250) after emp_address;
Explanation
In the above example, we use the change clause with the after option. The final output of the show databases query we illustrate by using the following snapshot
Conclusion
We hope from this article you have understood about the MariaDB rename column. From this article, we have learned the basic syntax of the rename column and we also see different example rename columns. From this article, we learned how and when we use MariaDB rename columns.
Recommended Articles
This is a guide to MariaDB rename column. Here we discuss the basic syntax of the rename column and we also see different example rename columns. You may also have a look at the following articles to learn more –