What Happens When We UPDATE an Indexed Column in Oracle

When we update an indexed column in Oracle, Oracle updates both the table data and the corresponding index entries to maintain consistency.

In simple words:

  1. Oracle finds the existing record.
  2. It updates the column value in the table.
  3. It removes the old index entry.
  4. It inserts a new index entry with the updated value.
  5. The index remains synchronized with the table.

Let's understand with a practical example.

1. Create an EMPLOYEE Table

First, create an EMPLOYEE table:

CREATE TABLE EMPLOYEE
(
    EMP_ID NUMBER,
    EMP_NAME VARCHAR2(50),
    DEPT_ID NUMBER,
    SALARY NUMBER
);

Insert some records:

INSERT INTO EMPLOYEE VALUES (101, 'Ravi', 10, 30000);
INSERT INTO EMPLOYEE VALUES (102, 'Kumar', 20, 40000);
INSERT INTO EMPLOYEE VALUES (103, 'Sita', 10, 50000);

COMMIT;

Our table looks like this:

EMP_ID EMP_NAME DEPT_ID SALARY
101Ravi1030000
102Kumar2040000
103Sita1050000

2. Create an Index on EMP_ID

Create a Normal Index on EMP_ID:

CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);

Conceptually, the index contains:

EMP_ID ROWID
101ROWID-1
102ROWID-2
103ROWID-3

Each index entry points to the corresponding table row.

Note: The ROWIDs shown in this example are illustrative. Actual Oracle ROWIDs identify physical row locations.

3. Now UPDATE the Indexed Column

Execute:

UPDATE EMPLOYEE
SET EMP_ID = 105
WHERE EMP_ID = 102;

COMMIT;

We changed EMP_ID from 102 to 105.

What Happens Internally?

Step 1: Oracle Finds the Existing Record

Oracle can use the index to locate EMP_ID = 102.

Step 2: Oracle Updates the Table

Before UPDATE:

EMP_ID EMP_NAME
102Kumar

After UPDATE:

EMP_ID EMP_NAME
105Kumar

Step 3: Oracle Updates the Index

Before UPDATE:

EMP_ID ROWID
101ROWID-1
102ROWID-2
103ROWID-3

After UPDATE:

EMP_ID ROWID
101ROWID-1
103ROWID-3
105ROWID-2

Notice what happened:

  • Old index entry 102 is removed.
  • New index entry 105 is inserted.
  • The ROWID continues to identify the same table row in this example.

4. UPDATE Operation Flow

Let's understand the complete UPDATE operation flow:

UPDATE EMPLOYEE SET EMP_ID = 105
↓
Find Existing Row: EMP_ID = 102
↓
Update Table Column: 102 → 105
↓
Remove Old Index Entry: 102
↓
Insert New Index Entry: 105
↓
Index Maintained Automatically

5. What Happens When We UPDATE a Non-Indexed Column?

This is another important interview concept.

Suppose we have an index only on EMP_ID.

Now execute:

UPDATE EMPLOYEE
SET SALARY = 60000
WHERE EMP_ID = 101;

COMMIT;

Here:

  • EMP_ID is indexed.
  • SALARY is not indexed.
  • We are updating SALARY only.

What Happens?

  1. Oracle finds the row using EMP_ID.
  2. Oracle updates SALARY in the table.
  3. The EMP_ID index entry generally does not need to change.
Important: Oracle generally does not need to modify an index when only a non-indexed column is updated, unless another applicable index or indexed expression depends on that column.

6. What Happens If We UPDATE a Column That Has Multiple Indexes?

Suppose we create two indexes:

CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);

CREATE INDEX IDX_EMP_NAME
ON EMPLOYEE(EMP_NAME);

Now execute:

UPDATE EMPLOYEE
SET EMP_NAME = 'Rahul'
WHERE EMP_ID = 101;

Oracle needs to:

  • Update the EMP_NAME value in the table.
  • Remove the old EMP_NAME index entry.
  • Insert the new EMP_NAME index entry.
  • Maintain the EMP_ID index as needed for the row. Since its key value has not changed, no key change is required.

7. Difference Between UPDATE Indexed and Non-Indexed Columns

Feature Update Indexed Column Update Non-Indexed Column
Table data updatedYesYes
Index maintenanceRequiredGenerally not required
Old index entryRemoved when key changesUnchanged
New index entryInserted when key changesNot required
DML overheadGenerally higherGenerally lower

8. What Happens If We UPDATE a Column in a Unique Index?

Suppose we have:

CREATE UNIQUE INDEX IDX_EMP_ID_UNQ
ON EMPLOYEE(EMP_ID);

Now execute:

UPDATE EMPLOYEE
SET EMP_ID = 101
WHERE EMP_ID = 105;

If EMP_ID = 101 already exists, Oracle returns:

ORA-00001: unique constraint violated

Why?

Because the updated value violates the unique index.

The UPDATE statement fails, and the existing value remains unchanged unless other transaction operations affect it.

Interview Point: A unique index prevents duplicate non-NULL key values. If an UPDATE attempts to create a duplicate key, Oracle raises ORA-00001.

9. Important Oracle Interview Questions

Q1. What happens when we update an indexed column?

Oracle updates the table row, removes the old index entry and inserts a new index entry containing the updated value.

Q2. What happens when we update a non-indexed column?

Oracle updates the table row, but generally does not need to modify indexes whose key values are unaffected.

Q3. Does Oracle automatically maintain indexes during UPDATE?

Yes. Oracle automatically maintains applicable indexes.

Q4. Does updating an indexed column affect performance?

Yes. It generally involves additional index-maintenance work compared with updating a non-indexed column.

Q5. What happens if the new value already exists in a Unique Index?

Oracle raises ORA-00001 and rejects the update.

Q6. Do we need to rebuild an index after UPDATE?

No. Oracle automatically maintains the index. Rebuilding after every UPDATE is unnecessary.

10. Difference Between INSERT, UPDATE and DELETE on Indexed Tables

DML Operation What Happens to Index?
INSERTNew index entry is inserted.
UPDATE indexed columnOld entry is removed and new entry is inserted.
UPDATE non-indexed columnGenerally no index key change.
DELETECorresponding index entries are removed.

11. Easy Way to Remember for Interviews

UPDATE Indexed Column = Table Update + Index Maintenance

  • Oracle finds the existing row.
  • Oracle updates the table column.
  • Old index key entry is removed when the key changes.
  • New index key entry is inserted.
  • Oracle automatically maintains index consistency.

One-Line Interview Answer

"When we update an indexed column in Oracle, the database updates the table row, deletes the old index key entry and inserts the new index key entry to maintain index consistency."

Next Important Interview Topic

What Happens Internally When We DELETE Data from an Indexed Table in Oracle?

Understanding INSERT, UPDATE and DELETE operations on indexed tables is very important for Oracle SQL performance tuning interviews.

No comments:

Post a Comment