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:
- Oracle finds the existing record.
- It updates the column value in the table.
- It removes the old index entry.
- It inserts a new index entry with the updated value.
- 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 |
|---|---|---|---|
| 101 | Ravi | 10 | 30000 |
| 102 | Kumar | 20 | 40000 |
| 103 | Sita | 10 | 50000 |
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 |
|---|---|
| 101 | ROWID-1 |
| 102 | ROWID-2 |
| 103 | ROWID-3 |
Each index entry points to the corresponding table row.
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 |
|---|---|
| 102 | Kumar |
After UPDATE:
| EMP_ID | EMP_NAME |
|---|---|
| 105 | Kumar |
Step 3: Oracle Updates the Index
Before UPDATE:
| EMP_ID | ROWID |
|---|---|
| 101 | ROWID-1 |
| 102 | ROWID-2 |
| 103 | ROWID-3 |
After UPDATE:
| EMP_ID | ROWID |
|---|---|
| 101 | ROWID-1 |
| 103 | ROWID-3 |
| 105 | ROWID-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:
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?
- Oracle finds the row using EMP_ID.
- Oracle updates SALARY in the table.
- The EMP_ID index entry generally does not need to change.
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 updated | Yes | Yes |
| Index maintenance | Required | Generally not required |
| Old index entry | Removed when key changes | Unchanged |
| New index entry | Inserted when key changes | Not required |
| DML overhead | Generally higher | Generally 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.
9. Important Oracle Interview Questions
Oracle updates the table row, removes the old index entry and inserts a new index entry containing the updated value.
Oracle updates the table row, but generally does not need to modify indexes whose key values are unaffected.
Yes. Oracle automatically maintains applicable indexes.
Yes. It generally involves additional index-maintenance work compared with updating a non-indexed column.
Oracle raises ORA-00001 and rejects the 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? |
|---|---|
| INSERT | New index entry is inserted. |
| UPDATE indexed column | Old entry is removed and new entry is inserted. |
| UPDATE non-indexed column | Generally no index key change. |
| DELETE | Corresponding 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