Lab 09
Lab 09
9-2
Data Manipulation Language
9-3
Adding a New Row to a Table
New
DEPARTMENTS row
9-4
INSERT Statement
Syntax
• Add new rows to a table by using the INSERT
statement:
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
9-5
Inserting New Rows
9-6
Inserting Rows with Null Values
9-7
Inserting Special Values
9-8
Inserting Specific Date and Time Values
9-9
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 - 10
Lesson Agenda
9 - 11
Changing Data in a Table
EMPLOYEES
9 - 12
UPDATE Statement
Syntax
• Modify existing values in a table with the UPDATE
statement:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
9 - 13
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 WHERE clause:
UPDATE copy_emp
SET department_id = 110;
9 - 14
Updating Two Columns with a Subquery
9 - 15
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
employee_id = 100)
WHERE job_id WHERE
= (SELECT job_id
FROM
employees
WHERE employee_id
= 200);
9 - 16
Lesson Agenda
9 - 17
Removing a Row from a Table
DEPARTMENTS
9 - 18
DELETE
Statement
You can remove existing rows from a table by using the
DELETE statement:
9 - 19
Deleting Rows from a Table
• All rows in the table are deleted if you omit the WHERE
clause:
9 - 20
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 - 21
TRUNCATE
Statement
• Removes all rows from a table, leaving the table empty
• Is a data definition language (DDL) statement rather than a
DML statement; cannot easily be undone
• Syntax:
9 - 22
Lesson Agenda
9 - 23
Database Transactions
9 - 24
Database Transactions: Start and End
9 - 25
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 - 26
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 - 27
Rolling Back Changes to a Marker
INSERT...
ROLLBACK TO update_done;
9 - 28
Implicit Transaction Processing
9 - 29
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.
9 - 33
State of the Data After COMMIT
9 - 34
Committing Data
9 - 35
State of the Data After ROLLBACK
9 - 36
State of the Data After ROLLBACK: Example
ROLLBACK;
Rollback complete.
COMMIT;
Commit complete.
9 - 37
Statement-Level Rollback
9 - 38
Lesson Agenda
9 - 39
Read Consistency
9 - 40
Implementing Read Consistency
User A
Undo
segments
Changed
SELECT * Data
FROM userA.employees; Read- and data
consistent Before
image change
(“old” data)
User B
9 - 41
Lesson Agenda
9 - 42
FOR UPDATE Clause in a SELECT
Statement
• Locks the rows in the EMPLOYEES table where job_id
is
SA_REP.
SELECT employee_id, salary, commission_pct, job_id
FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE
ORDER BY employee_id;
9 - 43
FOR UPDATE Clause:
Examples
• You can use the FOR UPDATE clause in a
SELECT
statement against multiple tables.
SELECT e.employee_id, e.salary, e.commission_pct
FROM employees e JOIN departments d
USING (department_id)
WHERE job_id = 'ST_CLERK‘
AND location_id = 1500
FOR UPDATE
ORDER BY e.employee_id;
9 - 44
Quiz
1. True
2. False
9 - 46
Summary
9 - 47
Thank You