EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials DB2 Tutorial DB2 rename column
Secondary Sidebar
DB2 Tutorial
  • DB2 Tutorial
    • What is DB2?
    • DB2? current date
    • DB2 purescale
    • DB2 backup
    • DB2 restore
    • DB2 C Express
    • DB2 Version
    • DB2? Architecture
    • DB2? Data Types
    • DB2? load
    • DB2? order by
    • DB2 date
    • DB2 NVL
    • DB2? update
    • DB2 warehouse
    • DB2 grant
    • DB2 database
    • DB2 VARCHAR
    • DB2? INSERT
    • DB2 LISTAGG
    • DB2 LIKE
    • DB2 TRUNCATE TABLE
    • DB2 LIST TABLES
    • DB2 between
    • DB2? current timestamp
    • DB2? length
    • DB2? bind
    • DB2 limit rows
    • DB2? export
    • DB2 with
    • DB2 Create Table
    • DB2 case statement
    • DB2 CAST
    • DB2 Functions
    • DB2 Date Functions
    • DB2? row_number
    • DB2 trim
    • DB2? Translate
    • DB2 UNION
    • DB2 timestamp
    • DB2? TIMESTAMPDIFF
    • DB2? replace
    • DB2 merge
    • DB2 COALESCE
    • DB2 ISNULL
    • DB2? explain
    • DB2 Join
    • DB2 alter column
    • DB2 rename column
    • DB2? Describe Table
    • DB2? rename table
    • DB2 List Databases
    • DB2 LUW
    • DB2 Query
    • DB2 GROUP BY
    • DB2 TO_DATE

DB2 rename column

DB2 rename column

Introduction to DB2 rename column

DB2 rename column is the statement used in DB2 to change the name of the existing column in the particular table of the current database to a new column name. Due to change in the business requirements or often giving non-meaningful names or sometimes by mistakenly specifying the wrong spelling of the name while creating the table might cause a need to change the name of the column. In this article, we will study how we can make use of the RENAME COLUM statement along with ALTER TABLE statement to modify the name of the existing column to a new one. We will also study its syntax and learn about its implementation along with certain examples.

Syntax

The syntax of the DB2 rename column statement is as shown below –

ALTER TABLE <name of table> RENAME COLUMN owner <name of column > TO <new column name>

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Alternatively, we can also make use of the RENAME COLUMN statement, which is the extension of SQL in the format of ANSI and ISO standards. The following syntax shows us the syntax of the RENAME COLUMN statement –

RENAME COLUMN owner <name of table>. < name of column > TO <new column name>

We have to give a period in the above syntax between the name of the table and the name of the column. The specification of the owner name is again optional in the above syntax as well.

In both of the above syntax, the keywords and the terminologies that are used are as explained below –

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

Name of the table – This should be the table name that should actually exist in the current database that we are using.

Name of the column – This should be the existing column name that exists in the table which we have specified previously.

Owner – This is the name of the owner who has ownership of that particular table. The specification of the owner is optional.

New column name – This can be any name that is an alphanumeric string that can even contain any of the special symbols in it. The column name should not exist in that table and should be unique in the table that we are trying to change it to.

Prerequisites –

We can make the use of ALTER COLUMN command to rename a particular column only if we have certain conditions that are accepted and the scenario is favorable. The first restriction is that you should be the owner of the table or at least have the privileges of ALTER on that table. You should possess the privilege of DBA on the database.

Examples

In order to understand the implementation of the ALTER COLUMN statement, let’s take some examples to see how it can be used.

Example #1

Consider one table named employee_details containing the details of all the employees present in a particular company. Suppose that the contents of the employee details table are retrieved using the following sql query –

SELECT * FROM employee_details;

The execution of the above query statement is shown below, showing us different columns present in the employee_Details table and the rows and records stored in that table.

DB2 rename column output 1

Now, suppose that I want to modify the column name mobile_number to contact_number. In this case, we can make use of any of the above-mentioned syntaxes. If we use the first syntax, then the query statement will become as shown below –

ALTER TABLE employee_details RENAME COLUMN mobile_number TO contact_number
SELECT * FROM employee_details;

The execution of the above query statement gives out the following results if we try to execute the same select query after the execution of the above query statement, which shows that the column name has been changed to the contact number that was previously with the name of the mobile number.

DB2 rename column output 2

Alternatively, if we make the use of the second syntax then for renaming the same column, then the query statement will be as shown below –

RENAME COLUMN employee_details.mobile_number TO contact_number;
SELECT * FROM employee_details;

The execution of the above query statement also modifies the name of the column and shows us the same output after selection of the table contents as shown in the above picture of employee details table –

DB2 rename column output 3

Example #2

Let us take one more example, consider the existing table named contact details that have the following contents in it. The records can be retrieved by using the following select query –

SELECT * FROM contact_details;

The execution of the above query statement gives out the following output showing all the column names of the contact details table and also the rows containing records present inside the table –

output 4

Now suppose we want to change the name of the column alternative_address to address2. This can be done by using the following query statement –

ALTER TABLE contact_details RENAME COLUMN alternative_address TO address2;
SELECT * FROM employee_details;

The execution of the above query statement gives out the following results if we try to execute the same select query after the execution of the above query statement, which shows that the column name has been changed to the contact number that was previously with the name of the mobile number.

output 5

Alternatively, if we make the use of the second syntax then for renaming the same column, then the query statement will be as shown below –

RENAME COLUMN contact_details.alternative_address TO address2;
SELECT * FROM employee_details;

The execution of the above query statement also modifies the name of the column and shows us the same output after selection of the table contents as shown in the above picture of contact details table –

output 6

Conclusion – DB2 rename column

We can use the RENAME COLUMN statement or ALTER TABLE ALTER COLUMN statement to rename a particular column in the table of the current database. We have to keep in mind certain prerequisites that need to follow before using any of the above two syntaxes for renaming the column, like you should be the owner of the table or at least have altered privileges on the table. Changing the column name does not affect the contents of the table. The RENAME COLUMN is the ISO standard for using the alternative of ALTER TABLE ALTER COLUMN command.

Recommended Articles

This is a guide to the DB2 rename column. Here we discuss the implementation of the DB2 rename column along with certain examples. You may also have a look at the following articles to learn more –

  1. DB2 LISTAGG
  2. DB2 INSERT
  3. DB2 Interview Questions
  4. Database Management Software
Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)
  7 Online Courses |  8 Hands-on Projects |  73+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

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