Open In App

How to Rename a Column in PL/SQL?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In PL/SQL, renaming the column name plays a crucial role in the database management. It is performed the enhance clarity, maintain consistency, or accommodate evolving the requirements of the data.

Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column names. PL/SQL can provide the direct syntax for executing the renaming of the column operations within the Oracle database.

How to Rename a Column in PL/SQL

Rename the column name in PL/SQL involved the ALTER TABLE statement for modifying the structure of the existing table. This enables the users to rename a column name without changing the datatype or other attributes of the existing column.

Syntax for rename a column in PL/SQL:

ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

Explanation of Syntax:

  • ALTER TABLE keyword indicates the altering of the structure of an existing table.
  • table_name specifies the name of the table that contains the column you want to be altering the column name.
  • RENAME COLUMN clause describes the Oracle that you want to be altering the name of the specific column in the table.
  • old_column_name represents the present name of the column you want to change, and this is the column name that already exists in the table.
  • new_column_name denotes the new name for the existing column. This is the new name for changing the column name after the rename.

Note: If you change the column name, it does not effect its data type or any other attributes. Be careful that any dependencies like views, procedures or triggers that may affect by change the column name and update accordingly.

Example of How to Rename a column in PL/SQL

Example 1: Rename a column, Ename to Empname in EMP table in PL/SQL

Step 1: Create a table in PL/SQL. (Example: emp )

Step 2: Retrieve the table data before altering the column name in a table using the below syntax.

Select * from table_name;

Example:

Before rename the column name

Before rename the column name

Step 3: You can use the ALTER TABLE statement to altering the ENAME column to the EMPNAME by using the below syntax.

ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

Example:

Altering the column name

Altering the column name

Step 4: After altering, we query the emp table again to verifying the column name has been changed or not. The output will show the altered column ENAME to EMPNAME.

Output:

After rename the column name

After rename the column name

Example 2: Rename a column, Sal to Salary in the EMP table in PL/SQL

Step 1: Create a table in PL/SQL. (Example: emp)

Step 2: Retrieve the table data before altering the column name in a table using the below syntax.

Select * from table_name;

Example:

Before rename the column name

Before rename the column name

Step 3: You can use the ALTER TABLE statement to rename the Sal column to the Salary by using the below syntax.

ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

Example:

Altering the column name

Altering the column name

Step 4: After altering, we query the emp table again to verifying the column name has been changed or not. The output will show the altered column SAL to SALARY.

Output:

After rename the column name

After rename the column name

Conclusion

In Conclusion, rename a column name in the PL/SQL involved to make sure that integrity of the data and consistency of the data with in the database schema. If the developers can follow the above steps developers can be sufficient to rename the columns without compromising the integrity of the existing data or interrupting the database operations. Recently the PL/SQL robust features for manipulation of the data and modification of the schema enabled the seamless column renaming while maintaining the overall stability of the database system.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads