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:
- Oracle identifies the record to be deleted.
- It deletes the row from the table logically.
- It removes the corresponding entries from all applicable indexes.
- 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 |
|---|---|---|---|
| 101 | Ravi | 10 | 30000 |
| 102 | Kumar | 20 | 40000 |
| 103 | Sita | 10 | 50000 |
| 104 | Raju | 30 | 60000 |
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.
101, 102, 103, 104
Indexed EMP_ID values and ROWIDs
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 |
|---|---|---|
| 102 | Kumar | 20 |
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 |
|---|---|
| 101 | ROWID-1 |
| 102 | ROWID-2 |
| 103 | ROWID-3 |
| 104 | ROWID-4 |
After DELETE – IDX_EMP_ID:
| EMP_ID | ROWID |
|---|---|
| 101 | ROWID-1 |
| 103 | ROWID-3 |
| 104 | ROWID-4 |
Similarly, Oracle removes the corresponding entry from IDX_EMP_DEPT.
4. DELETE Operation Flow
Let's understand the complete DELETE operation flow:
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.
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 table | Delete the row |
| IDX_EMP_ID | Remove corresponding entry |
| IDX_EMP_NAME | Remove corresponding entry |
| IDX_EMP_DEPT | Remove corresponding entry |
| IDX_EMP_SALARY | Remove corresponding entry |
| IDX_EMP_EMAIL | Remove corresponding entry |
All applicable index entries must be maintained.
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.
9. Important Oracle Interview Questions
Oracle removes the corresponding index entries automatically.
No. Oracle automatically maintains indexes during DELETE operations.
Index changes are performed as part of the DELETE transaction. They become permanent after COMMIT.
Yes, before COMMIT.
Example:
DELETE FROM EMPLOYEE
WHERE EMP_ID = 102;
ROLLBACK;
The deleted row is restored.
No. DELETE removes applicable index entries, not the index structure.
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