How to Rename a Column Name in MariaDB?
Last Updated :
08 Feb, 2024
MariaDB is an open-source relational database management system that can be used to create databases, and tables and to query the tables. Anyone who knows how to write SQL queries can use any of the databases like MariaDB or Mysql for the execution of SQL queries. In this article, we will learn about How to rename a column name in MariaDB with the help of various examples and so on.
Introduction to Rename Column in MariaDB
We change the file names in our system or mobile so that we can remember them and access them faster and easily. Similarly, when creating tables and columns, it is always better to name the table or columns short and relevant to the data it is storing so that the data can be retrieved easily. So, how to rename the column name after creating the table? In MariaDB, we can rename the table name and column name by using the alter statement.
ALTER command is a data definition language (DDL) statement that can be used for modifying the structure of tables in a database i.e., add, drop, or modify columns in a table. Alter command can also be used for renaming a table by using the 'RENAME TO' keyword. For renaming a table or column we need to create a table with some columns and insert records.
Examples of How to Rename a Column Name in MariaDB
To understand How to Rename a Column Name in MariaDB we need a table on which we will perform various queries and operations. In this article, we have an Employee table which is shown below.
EmployeeID
| FirstName
| LastName
| Salary
|
---|
1
| John
| Doe
| 50000
|
2
| Jane
| Smith
| 65000
|
3
| Michael
| Johnson
| 40000
|
4
| Emily
| Brown
| 55000
|
5
| Chris
| Wilson
| 70000
|
Example 1: Using ALTER TABLE RENAME tO Statement
Syntax:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
Suppose we want to change the column name EmployeeID to EmpID.
Query:
ALTER TABLE Employees RENAME COLUMN EmployeeID TO EmpID;
Output:
Alter column rename to Explanation: In the above Query, We have changed the column of Employees Table from EmployeeID TO EmpID.
Example 2: Using ALTER TABLE CHANGE Statement
The CHANGE keyword can also be used for renaming the column name in a table in MariaDB.
Syntax:
ALTER TABLE table_name CHANGE old_column_name new_column_name new_column_datatype;
The main difference between 'RENAME TO' and 'CHANGE' is, that we cannot directly change the column datatype by using the 'RENAME' keyword but the same is possible by using the 'CHANGE' keyword, we need to specify the new column name and the required datatype.
Now, We want to change the column name Salary to Emp_sal. The datatype of column Salary is INT. Let's change the datatype to BIGINT. We can specify the old column datatype or new datatype.
Query using No change in Datatype:
ALTER TABLE Employees CHANGE Salary Emp_sal INT;
Query using New Datatype:
ALTER TABLE table_name CHANGE Salary Emp_sal BIGINT;
Output:
Alter table changeTo view the details about the columns in a table and the corresponding datatypes, 'DESC table_name' can be used. this command describes the table.
Describe tableConclusion
The 'RENAME TO' and 'CHANGE' keywords can be used to rename the column name in MariaDB. By renaming or changing the column names, it improves the quality of the data in the table, which enhances the database organization. Alter command may also be used to add columns, drop columns, and modify the datatype of columns using 'ADD', 'DROP', and MODIFY'. The 'Alter' command and the 'Update' command are not the same. The former is a data definition language statement, which is used for changing the structure of the table, and the later is a data manipulation language statement used for manipulating the data or records in the table.
Similar Reads
How to Rename a Column in MySQL? Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
4 min read
How to Rename a Column in PL/SQL? Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How to Rename Multiple Columns in R Renaming columns in R Programming Language is a basic task when working with data frames, and it's done to make things clearer. Whether you want names to be more understandable, follow certain rules, or match your analysis, there are different ways to change column names. There are types of methods
4 min read
How to Set a Column Value to NULL in MariaDB In MariaDB, the NULL represents an unknown value in a column. Changing a column value to NULL is the most common operation performed in MariaDB that allows us to remove existing data in a specific field. It is applicable in different ways including data correction, record inclusions and values setti
4 min read
How to Rename a Column in View in PL/SQL? Renaming a column in a view can be a challenging task in PL/SQL, as the language does not provide a direct command for this operation. However, there are several effective methods to achieve this goal without compromising data integrity or the structure of the view. In This article, we will explore
4 min read