What Happens When We DELETE Records from an Indexed Table in Oracle

When we delete a record from an Oracle table that has indexes, Oracle deletes the corresponding row from the table and removes the associated index entries.

In simple words:

  1. Oracle identifies the record to be deleted.
  2. It deletes the row from the table logically.
  3. It removes the corresponding entries from all applicable indexes.
  4. Oracle maintains consistency between the table and its indexes.

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);
INSERT INTO EMPLOYEE VALUES (104, 'Raju', 30, 60000);

COMMIT;

Our table looks like this:

EMP_ID EMP_NAME DEPT_ID SALARY
101Ravi1030000
102Kumar2040000
103Sita1050000
104Raju3060000

2. Create Indexes on the Table

Let's create two indexes:

CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);

CREATE INDEX IDX_EMP_DEPT
ON EMPLOYEE(DEPT_ID);

Now we have:

  • EMPLOYEE table
  • IDX_EMP_ID
  • IDX_EMP_DEPT

Each index contains indexed values and corresponding ROWIDs.

EMPLOYEE TABLE

101, 102, 103, 104

↓
IDX_EMP_ID

Indexed EMP_ID values and ROWIDs

↓
IDX_EMP_DEPT

Indexed DEPT_ID values and ROWIDs

3. Now DELETE a Record

Execute:

DELETE FROM EMPLOYEE
WHERE EMP_ID = 102;

COMMIT;

We are deleting Kumar's record.

What Happens Internally?

Step 1: Oracle Identifies the Record

Oracle may use IDX_EMP_ID to locate EMP_ID = 102.

Step 2: Oracle Deletes the Table Row Logically

Before DELETE:

EMP_ID EMP_NAME DEPT_ID
102Kumar20

After DELETE:

The row is marked as deleted and is no longer visible to queries after the transaction commits.

Step 3: Oracle Removes the Corresponding Index Entries

Before DELETE – IDX_EMP_ID:

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

After DELETE – IDX_EMP_ID:

EMP_ID ROWID
101ROWID-1
103ROWID-3
104ROWID-4

Similarly, Oracle removes the corresponding entry from IDX_EMP_DEPT.

Important: The ROWIDs shown above are illustrative. Oracle automatically maintains actual index entries and row locations.

4. DELETE Operation Flow

Let's understand the complete DELETE operation flow:

DELETE FROM EMPLOYEE WHERE EMP_ID = 102
↓
Find the Matching Record
↓
Delete the Table Row Logically
↓
Remove from IDX_EMP_ID
↓
Remove from IDX_EMP_DEPT
↓
Table and Indexes Remain Consistent
Important: Oracle performs the DELETE and index maintenance within the same transaction. COMMIT makes the changes permanent; ROLLBACK can undo them.

5. What Happens If We DELETE Multiple Records?

Execute:

DELETE FROM EMPLOYEE
WHERE DEPT_ID = 10;

COMMIT;

This deletes all employees belonging to department 10.

Oracle must:

  • Identify all matching rows.
  • Delete those rows logically from the table.
  • Remove the corresponding entries from all applicable indexes.

If many rows are deleted, index maintenance can add significant overhead.

6. Does Oracle Physically Remove Deleted Records Immediately?

This is an important Oracle interview question.

No. A DELETE does not necessarily remove the row's bytes from the data block immediately.

Oracle generally marks the row as deleted and maintains undo information.

Important points:

  • Undo information allows transaction rollback.
  • Undo also supports read consistency.
  • After COMMIT, other transactions can no longer see the deleted rows.
  • Space in the table block can generally be reused by future inserts.
  • Index entries are logically removed, but their block space may not immediately be returned to the operating system.
Remember: COMMIT does not immediately shrink the table or return its allocated space to the operating system. It commits the transaction.

7. What Happens When We DELETE Data from a Table with Multiple Indexes?

Suppose EMPLOYEE has five indexes.

When one record is deleted:

Object Operation
EMPLOYEE tableDelete the row
IDX_EMP_IDRemove corresponding entry
IDX_EMP_NAMERemove corresponding entry
IDX_EMP_DEPTRemove corresponding entry
IDX_EMP_SALARYRemove corresponding entry
IDX_EMP_EMAILRemove corresponding entry

All applicable index entries must be maintained.

Important: More indexes generally mean more DELETE overhead because Oracle must maintain all applicable indexes.

8. Difference Between DELETE, TRUNCATE and DROP

This is a very important Oracle interview question.

Feature DELETE TRUNCATE DROP
Purpose Deletes selected or all rows Removes all rows Removes entire table
WHERE condition Supported Not supported Not supported
DML or DDL DML DDL DDL
Rollback Possible before COMMIT Cannot rollback normally Cannot rollback normally
Indexes Maintained Remain but are reset/maintained as empty structures Dropped with the table
Triggers DELETE triggers fire DELETE triggers do not fire Not applicable
Table structure Remains Remains Removed

DELETE Example

DELETE FROM EMPLOYEE
WHERE EMP_ID = 102;

Deletes only the matching employee.

TRUNCATE Example

TRUNCATE TABLE EMPLOYEE;

Removes all rows but keeps the table structure and its indexes.

DROP Example

DROP TABLE EMPLOYEE;

Removes the entire table, including its indexes.

Important Oracle Concept: TRUNCATE and DROP are DDL statements and cause implicit transaction commits. They cannot be rolled back like an ordinary DELETE. Oracle also has special recovery options, such as Flashback features, depending on the operation and configuration.

9. Important Oracle Interview Questions

Q1. What happens to indexes when we delete a record?

Oracle removes the corresponding index entries automatically.

Q2. Do we need to rebuild indexes after DELETE?

No. Oracle automatically maintains indexes during DELETE operations.

Q3. Does DELETE remove index entries immediately?

Index changes are performed as part of the DELETE transaction. They become permanent after COMMIT.

Q4. Can we ROLLBACK a DELETE statement?

Yes, before COMMIT.

Example:

DELETE FROM EMPLOYEE
WHERE EMP_ID = 102;

ROLLBACK;

The deleted row is restored.

Q5. Does DELETE remove the index itself?

No. DELETE removes applicable index entries, not the index structure.

Q6. What happens when we delete all records using DELETE?

All rows are deleted logically, and applicable index entries are removed. The table and index structures remain.

10. Easy Way to Remember for Interviews

Operation Table Index
INSERT Adds a row Adds index entries
UPDATE indexed column Updates a row Removes old entry and adds new entry
DELETE Deletes a row logically Removes corresponding entries

One-Line Interview Answer

"When we delete a record from an indexed table in Oracle, Oracle logically deletes the table row and automatically removes the corresponding entries from all applicable indexes. The index structures themselves remain."

Next Important Oracle Performance Topic

DELETE vs TRUNCATE vs DROP in Oracle

Understanding their internal behavior, undo generation, transaction handling, and index maintenance is very useful for Oracle DBA and SQL Developer interviews.

No comments:

Post a Comment