Basic Oracle Concepts and Programming Question:
Download Job Interview Questions and Answers PDF
How To Rename an Index in Oracle?
Answer:
Let's say you have an existing index, and you don't like its name anymore for some reason, you can rename it with the ALTER INDEX ... RENAME TO statement. Here is an example script on how to rename an index:
CREATE TABLE student (id NUMBER(5) PRIMARY KEY,
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL,
birth_date DATE NOT NULL,
social_number VARCHAR(80) UNIQUE NOT NULL);
Table created.
SELECT index_name, table_name, uniqueness
FROM USER_INDEXES WHERE table_name = 'STUDENT';
<pre>INDEX_NAME TABLE_NAME UNIQUENES
----------------------- --------------------- ---------
SYS_C004153 STUDENT UNIQUE
SYS_C004154 STUDENT UNIQUE</pre>
ALTER INDEX SYS_C004153 RENAME TO student_pk;
Statement processed.
SELECT index_name, table_name, uniqueness
FROM USER_INDEXES WHERE table_name = 'STUDENT';
<pre>INDEX_NAME TABLE_NAME UNIQUENES
----------------------- --------------------- ---------
STUDENT_PK STUDENT UNIQUE
SYS_C004154 STUDENT UNIQUE</pre>
CREATE TABLE student (id NUMBER(5) PRIMARY KEY,
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL,
birth_date DATE NOT NULL,
social_number VARCHAR(80) UNIQUE NOT NULL);
Table created.
SELECT index_name, table_name, uniqueness
FROM USER_INDEXES WHERE table_name = 'STUDENT';
<pre>INDEX_NAME TABLE_NAME UNIQUENES
----------------------- --------------------- ---------
SYS_C004153 STUDENT UNIQUE
SYS_C004154 STUDENT UNIQUE</pre>
ALTER INDEX SYS_C004153 RENAME TO student_pk;
Statement processed.
SELECT index_name, table_name, uniqueness
FROM USER_INDEXES WHERE table_name = 'STUDENT';
<pre>INDEX_NAME TABLE_NAME UNIQUENES
----------------------- --------------------- ---------
STUDENT_PK STUDENT UNIQUE
SYS_C004154 STUDENT UNIQUE</pre>
Download Oracle Database Interview Questions And Answers
PDF
Previous Question | Next Question |
How To Create a Table Index in Oracle? | How To Drop an Index in Oracle? |