Summary: in this tutorial, you’ll learn how to rename a column in Oracle using the ALTER TABLE RENAME COLUMN statement.
Introduction to the ALTER TABLE RENAME COLUMN statement #
To change the name of a column to a new one, you use the ALTER TABLE ... RENAME COLUMN statement:
ALTER TABLE table_name
RENAME COLUMN column1 TO new_column;Code language: SQL (Structured Query Language) (sql)In this syntax:
- First, specify the table name to which the column you want to rename belongs.
- Second, provide the column you want to change (
column1) after theRENAME COLUMNkeywords. - Third, specify the new name for the column you want to change after the
TOkeyword.
Renaming column example #
First, create a new table called sales_performances:
CREATE TABLE sales_performances(
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
sales_year NUMBER NOT NULL,
amount NUMBER(19,2) NOT NULL
);Code language: SQL (Structured Query Language) (sql)Second, change the amount column to sales_amount:
ALTER TABLE sales_performances
RENAME COLUMN amount TO sales_amount;Code language: SQL (Structured Query Language) (sql)Third, verify the changes by showing the structure of the sales_performances table:
DESC sales_performances;Code language: SQL (Structured Query Language) (sql)Output:
Name Null? Type
------------ -------- ------------
ID NOT NULL NUMBER
SALES_YEAR NOT NULL NUMBER
SALES_AMOUNT NOT NULL NUMBER(19,2)Code language: SQL (Structured Query Language) (sql)The output indicates that the statement has changed the amount column to sales_amount column successfully.
Summary #
- Use the
ALTER TABLE RENAME COLUMNstatement to rename a column.
Quiz #
Was this tutorial helpful?