Professional Documents
Culture Documents
Manipulating Data
Objectives
9-2
Lesson Agenda
9-3
Data Manipulation Language
9-4
Adding a New Row to a Table
New
DEPARTMENTS row
9-5
INSERT Statement Syntax
9-6
Inserting New Rows
9-7
Inserting Rows with Null Values
9-8
Inserting Special Values
9-9
Inserting Specific Date and Time Values
9 - 10
Creating a Script
9 - 11
Copying Rows
from Another Table
• Write your INSERT statement with a subquery:
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
9 - 12
Lesson Agenda
9 - 13
Changing Data in a Table
EMPLOYEES
9 - 14
UPDATE Statement Syntax
9 - 15
Updating Rows in a Table
UPDATE employees
SET department_id = 50
WHERE employee_id = 113;
• Values for all the rows in the table are modified if you omit
the WHEREcopy_emp
UPDATE clause:
SET department_id = 110;
9 - 17
Updating Rows Based
on Another Table
Use the subqueries in the UPDATE statements to update row
values in a table based on values from another table:
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
9 - 18
Lesson Agenda
9 - 19
Removing a Row from a Table
DEPARTMENTS
9 - 20
DELETE Statement
9 - 21
Deleting Rows from a Table
• All rows in the table are deleted if you omit the WHERE
clause:
9 - 22
Deleting Rows Based
on Another Table
Use the subqueries in the DELETE statements to remove rows
from a table based on values from another table:
DELETE FROM employees
WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name
LIKE '%Public%');
9 - 23
TRUNCATE Statement
• Removes all rows from a table, leaving the table empty and
the table structure intact
• Is a data definition language (DDL) statement rather than a
DML statement; cannot easily be undone
• Syntax:
TRUNCATE TABLE table_name;
• Example:
TRUNCATE TABLE copy_emp;
9 - 24
Lesson Agenda
9 - 25
Database Transactions
9 - 26
Database Transactions: Start and End
9 - 27
Advantages of COMMIT
and ROLLBACK Statements
With COMMIT and ROLLBACK statements, you can:
• Ensure data consistency
• Preview data changes before making changes permanent
• Group logically-related operations
9 - 28
Explicit Transaction Control Statements
Time COMMIT
Transaction
DELETE
SAVEPOINT A
INSERT
UPDATE
SAVEPOINT B
INSERT
ROLLBACK ROLLBACK ROLLBACK
to SAVEPOINT B to SAVEPOINT A
9 - 29
Rolling Back Changes to a Marker
INSERT...
ROLLBACK TO update_done;
9 - 30
Implicit Transaction Processing
9 - 31
State of the Data
Before COMMIT or ROLLBACK
• The previous state of the data can be recovered.
• The current user can review the results of the DML
operations by using the SELECT statement.
• Other users cannot view the results of the DML statements
issued by the current user.
• The affected rows are locked; other users cannot change the
data in the affected rows.
9 - 32
State of the Data After COMMIT
9 - 33
Committing Data
• COMMIT;
Commit the changes:
9 - 34
State of the Data After ROLLBACK
9 - 35
State of the Data After ROLLBACK: Example
ROLLBACK;
Rollback complete.
COMMIT;
Commit complete.
9 - 36
Statement-Level Rollback
9 - 37
Lesson Agenda
9 - 38
Read Consistency
9 - 39
Implementing Read Consistency
User A
Undo
segments
Changed
SELECT * and
FROM userA.employees; Read- unchanged
consistent data
image Before
change
User B (“old” data)
9 - 40
Lesson Agenda
9 - 41
FOR UPDATE Clause in a SELECT Statement
9 - 44
Practice 9: Overview
9 - 45