SQL Guide Chapter

Lesson 10A – Managing Tables Using DML Statements in Oracle (or: how to change the data without regretting everything)

And look, reading data is safe. It’s like browsing a store: you can stare at products all day and nothing changes. DML is when you start rearranging shelves, throwing things out, and repainting the walls—except the “wal...

oracle 19c mysql notes study chapter

And look, reading data is safe. It’s like browsing a store: you can stare at products all day and nothing changes. DML is when you start rearranging shelves, throwing things out, and repainting the walls—except the “walls” are shared by everyone and the consequences are permanent unless you manage transactions correctly.

This lesson is about making changes on purpose and being able to undo them when you inevitably change the wrong thing.

You will learn to:


1. DML and Transactions: What’s at Stake

DML (Data Manipulation Language) statements:

A transaction is a set of DML statements that together form a logical unit of work, for example:

You either want all of that to succeed, or none of it. That’s what COMMIT and ROLLBACK are for.


2. INSERT – Getting New Rows into a Table

2.1 Basic INSERT ... VALUES

Best practice: name the columns, then provide matching values.

INSERT INTO departments (department_id, department_name, manager_id, location_id)
    VALUES (70, 'Public Relations', 100, 1700);

Rules:

You can omit the column list only if you provide values for every column in the table, in the table’s default column order:

INSERT INTO departments
    VALUES (80, 'Sales', 149, 2500);

This is legal but fragile—if a column is added or reordered, this breaks.

2.2 Inserting NULL values

Two ways to get NULL into a column:

  INSERT INTO demo (id)
      VALUES (2);   -- name column becomes NULL
  INSERT INTO departments (department_id, department_name, manager_id, location_id)
      VALUES (100, 'Finance', NULL, NULL);

Works as long as the column does not have a NOT NULL constraint.

2.3 Inserting dates and special values

Use date functions or TO_DATE to avoid ambiguity:

INSERT INTO employees (employee_id, last_name, hire_date, salary)
    VALUES (300, 'Nguyen', CURRENT_DATE, 5000);
    
    INSERT INTO employees (employee_id, last_name, hire_date, salary)
    VALUES (301, 'Lee', TO_DATE('2016-02-01', 'YYYY-MM-DD'), 4500);

2.4 INSERT with a subquery (INSERT ... SELECT)

You can insert multiple rows at once by selecting from another table.

INSERT INTO sales_reps (id, name, salary, commission_pct)
    SELECT employee_id,
           last_name,
           salary,
           commission_pct
    FROM   employees
    WHERE  job_id = 'SA_REP';

3. UPDATE – Changing Existing Rows (Carefully)

UPDATE modifies existing rows. The dangers live mostly in the WHERE clause.

3.1 Basic UPDATE

UPDATE employees
    SET    department_id = 50
    WHERE  employee_id   = 113;

If you omit the WHERE clause:

UPDATE employees
    SET    department_id = 10;

3.2 Updating multiple columns

UPDATE employees
    SET    salary   = 6000,
           job_id   = 'SA_REP'
    WHERE  employee_id = 113;

You can even use subqueries to copy values from another row:

-- Make employee 103’s job and salary match employee 205
    UPDATE employees
    SET   (job_id, salary) = (
            SELECT job_id, salary
            FROM   employees
            WHERE  employee_id = 205
          )
    WHERE employee_id = 103;

Be sure the subquery returns exactly one row.


4. DELETE and TRUNCATE – Removing Rows

4.1 DELETE

DELETE removes rows from a table.

DELETE FROM departments
    WHERE  department_name = 'Finance';

Without a WHERE clause:

DELETE FROM demo;

You can use subqueries to drive deletions:

DELETE FROM employees
    WHERE  department_id IN (
             SELECT department_id
             FROM   departments
             WHERE  department_name LIKE 'Public%'
           );

4.2 TRUNCATE

TRUNCATE is like DELETE without a WHERE clause, but more final and much faster.

TRUNCATE TABLE demo;

Why use it?


5. Transaction Control: COMMIT, ROLLBACK, SAVEPOINT

A transaction starts with the first DML after the last COMMIT/ROLLBACK and ends when you:

5.1 COMMIT

INSERT INTO demo VALUES (1, 'Mickey');
    INSERT INTO demo VALUES (2, 'Mary');
    COMMIT;

Effects:

5.2 ROLLBACK

INSERT INTO demo VALUES (3, 'Larry');
    ROLLBACK;

Effects:

Rollbacks have no effect on changes that have already been committed.

5.3 SAVEPOINT

SAVEPOINT lets you set intermediate markers within a transaction.

Example:

INSERT INTO demo VALUES (1, 'Mickey');
    INSERT INTO demo VALUES (2, 'Mary');
    SAVEPOINT before_mass_update;
    
    UPDATE demo
    SET    name = 'Bentley';   -- oops, too broad
    
    ROLLBACK TO before_mass_update;

Multiple savepoints are allowed, but rolling back to an earlier savepoint discards any savepoints set after it.

5.4 Implicit vs explicit transaction boundaries

Implicit commits happen when you execute:

Implicit rollbacks happen when:

Until you commit:


6. Row Locking and SELECT ... FOR UPDATE

Oracle uses row‑level locking:

To explicitly lock rows while you inspect them, you can use:

SELECT *
    FROM   demo
    FOR UPDATE;

Effects:

You can limit locking to certain columns/tables in joins:

SELECT e.employee_id,
           e.salary,
           d.department_name
    FROM   employees e
    JOIN   departments d
           ON e.department_id = d.department_id
    FOR UPDATE OF e.salary;

6.1 FOR UPDATE with WAIT

If another session already holds a lock, your FOR UPDATE will wait indefinitely—unless you set a timeout:

SELECT *
    FROM   demo
    FOR UPDATE WAIT 5;

This prevents your session from hanging forever because someone went to lunch holding a lock.

6.2 LOCK TABLE

LOCK TABLE lets you explicitly lock one or more tables in a specific mode:

LOCK TABLE demo IN EXCLUSIVE MODE;

Use this sparingly; row‑level locks are usually enough.


7. Read Consistency – What Other Sessions See

Oracle guarantees that queries see a consistent snapshot of data:

Example:

Session B sees the old salary until Session A commits. This avoids “partial” views of data mid‑transaction.


8. What You Should Now Be Able to Do

By the end of this lesson, you should be able to:

You’re now trusted not just to read from the database, but to change it—and more importantly, to back out your mistakes before everyone else sees them.

← previous view source markdown next →