Oracle – 14 – DML and Transaction Management

Data Manipulation Language (DML) and transaction management are essential aspects of working with Oracle databases. DML refers to the SQL commands and operations used to manipulate data within database tables, while transaction management deals with controlling the consistency and integrity of data changes. Here’s a brief description of Oracle DML and transaction management:

Data Manipulation Language (DML):

  1. INSERT Statement:
    • The INSERT statement is used to add new rows of data into a database table. You specify the target table and provide values or use a SELECT statement to insert data from another table.INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (101, ‘John Doe’, 55000);
  2. UPDATE Statement:
    • The UPDATE statement modifies existing data within a table. You specify the target table, set new values for one or more columns, and use a WHERE clause to specify the rows to update.
    UPDATE products SET price = price * 1.1 WHERE category = 'Electronics';
  3. DELETE Statement:
    • The DELETE statement removes rows from a table based on a specified condition. It is used to delete unwanted or obsolete data.
    DELETE FROM orders WHERE order_date < TO_DATE('2023-01-01', 'YYYY-MM-DD');
  4. MERGE Statement:
    • The MERGE statement is used to perform conditional updates or inserts based on a specified condition. It is useful for upsert operations (insert if not exists, update if exists).
    MERGE INTO target_table USING source_table ON (target_table.id = source_table.id) WHEN MATCHED THEN UPDATE SET target_table.column1 = source_table.column1 WHEN NOT MATCHED THEN INSERT (column1, column2) VALUES (source_table.column1, source_table.column2);

Transaction Management:

  1. Transaction Control Statements:
    • Oracle provides transaction control statements to manage the start, end, and rollback of transactions:
      • COMMIT: Saves all changes made during the current transaction to the database, making them permanent.ROLLBACK: Undoes all changes made during the current transaction, restoring the database to its previous state.SAVEPOINT: Sets a savepoint within a transaction, allowing you to roll back to that point if needed.SET TRANSACTION: Defines transaction attributes like isolation level and transaction name.
    BEGIN -- Start a transaction INSERT INTO table1 (column1) VALUES ('value1'); SAVEPOINT my_savepoint; -- Perform more operations ROLLBACK TO my_savepoint; -- Roll back to the savepoint -- Perform additional operations or COMMIT to make changes permanent END;
  2. Autocommit Mode:
    • By default, Oracle operates in autocommit mode, where each SQL statement is treated as a separate transaction, and changes are immediately committed. You can disable autocommit to group multiple statements into a single transaction.
    SET AUTOCOMMIT OFF;
  3. Isolation Levels:
    • Oracle supports different isolation levels, such as READ COMMITTED, SERIALIZABLE, and more, to control the visibility of changes made by one transaction to other concurrent transactions.
  4. Transaction Logs and Redo Logs:
    • Oracle maintains transaction logs and redo logs to ensure data consistency and durability. These logs record all changes made during transactions, allowing for recovery in case of system failures.

DML and transaction management are fundamental concepts in Oracle database management. Properly managing transactions ensures data integrity, while DML operations are essential for manipulating and maintaining the data stored in the database. Effective use of these features is critical for building reliable and robust database applications.